pkg/program: Un-export all subprograms.

This commit is contained in:
Qi Xiao 2020-01-09 10:20:07 +00:00
parent 167032c785
commit 720ae6ae7a
5 changed files with 30 additions and 40 deletions

View File

@ -8,21 +8,10 @@ import (
"github.com/elves/elvish/pkg/buildinfo"
)
// ShowVersion shows the version.
type ShowVersion struct{}
type buildInfoProgram struct{ JSON bool }
func (ShowVersion) Main(fds [3]*os.File, _ []string) int {
fmt.Fprintln(fds[1], buildinfo.Version)
return 0
}
// ShowBuildInfo shows build information.
type ShowBuildInfo struct {
JSON bool
}
func (info ShowBuildInfo) Main(fds [3]*os.File, _ []string) int {
if info.JSON {
func (p buildInfoProgram) Main(fds [3]*os.File, _ []string) int {
if p.JSON {
fmt.Fprintf(fds[1],
`{"version":%s,"goversion":%s,"reproducible":%v}`+"\n",
quoteJSON(buildinfo.Version), quoteJSON(runtime.Version()),
@ -34,3 +23,10 @@ func (info ShowBuildInfo) Main(fds [3]*os.File, _ []string) int {
}
return 0
}
type versionProgram struct{}
func (versionProgram) Main(fds [3]*os.File, _ []string) int {
fmt.Fprintln(fds[1], buildinfo.Version)
return 0
}

View File

@ -7,13 +7,10 @@ import (
"github.com/elves/elvish/pkg/program/daemon"
)
// Daemon runs the daemon subprogram.
type Daemon struct {
inner *daemon.Daemon
}
type daemonProgram struct{ inner *daemon.Daemon }
func (d Daemon) Main(fds [3]*os.File, _ []string) int {
err := d.inner.Main(daemonsvc.Serve)
func (p daemonProgram) Main(fds [3]*os.File, _ []string) int {
err := p.inner.Main(daemonsvc.Serve)
if err != nil {
logger.Println("daemon error:", err)
return 2

View File

@ -131,16 +131,16 @@ type Program interface {
func FindProgram(flag *flagSet) Program {
switch {
case flag.Help:
return ShowHelp{flag}
return helpProgram{flag}
case flag.Version:
return ShowVersion{}
return versionProgram{}
case flag.BuildInfo:
return ShowBuildInfo{flag.JSON}
return buildInfoProgram{flag.JSON}
case flag.Daemon:
if len(flag.Args()) > 0 {
return ShowCorrectUsage{"arguments are not allowed with -daemon", flag}
return badUsageProgram{"arguments are not allowed with -daemon", flag}
}
return Daemon{inner: &daemon.Daemon{
return daemonProgram{&daemon.Daemon{
BinPath: flag.Bin,
DbPath: flag.DB,
SockPath: flag.Sock,
@ -148,10 +148,10 @@ func FindProgram(flag *flagSet) Program {
}}
case flag.Web:
if len(flag.Args()) > 0 {
return ShowCorrectUsage{"arguments are not allowed with -web", flag}
return badUsageProgram{"arguments are not allowed with -web", flag}
}
if flag.CodeInArg {
return ShowCorrectUsage{"-c cannot be used together with -web", flag}
return badUsageProgram{"-c cannot be used together with -web", flag}
}
return &web.Web{
BinPath: flag.Bin, SockPath: flag.Sock, DbPath: flag.DB,

View File

@ -53,17 +53,17 @@ var findProgramTests = []struct {
}},
{[]string{"-daemon", "-bin", "/elvish"}, func(p Program) bool {
return p.(Daemon).inner.BinPath == "/elvish"
return p.(daemonProgram).inner.BinPath == "/elvish"
}},
{[]string{"-daemon", "-db", "/db"}, func(p Program) bool {
return p.(Daemon).inner.DbPath == "/db"
return p.(daemonProgram).inner.DbPath == "/db"
}},
{[]string{"-daemon", "-sock", "/sock"}, func(p Program) bool {
return p.(Daemon).inner.SockPath == "/sock"
return p.(daemonProgram).inner.SockPath == "/sock"
}},
}
func isDaemon(p Program) bool { _, ok := p.(Daemon); return ok }
func isDaemon(p Program) bool { _, ok := p.(daemonProgram); return ok }
func isWeb(p Program) bool { _, ok := p.(*web.Web); return ok }
func isShell(p Program) bool { _, ok := p.(*shell.Shell); return ok }

View File

@ -2,22 +2,19 @@ package program
import "os"
// ShowHelp shows help message.
type ShowHelp struct {
flag *flagSet
}
type helpProgram struct{ flag *flagSet }
func (s ShowHelp) Main(fds [3]*os.File, _ []string) int {
usage(fds[1], s.flag)
func (p helpProgram) Main(fds [3]*os.File, _ []string) int {
usage(fds[1], p.flag)
return 0
}
type ShowCorrectUsage struct {
type badUsageProgram struct {
message string
flag *flagSet
}
func (s ShowCorrectUsage) Main(fds [3]*os.File, _ []string) int {
usage(fds[2], s.flag)
func (p badUsageProgram) Main(fds [3]*os.File, _ []string) int {
usage(fds[2], p.flag)
return 2
}