lib: Split off the CLI portions into a separate package
This cleans things up a bit more and forces the lib package to not contain any accidental CLI parsing code.
This commit is contained in:
@@ -15,7 +15,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
// Package cli handles all of the core command line parsing. It's the first
|
||||
// entry point after the real main function, and it imports and runs our core
|
||||
// "lib".
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -32,6 +35,12 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Flags are some constant flags which are used throughout the program.
|
||||
type Flags struct {
|
||||
Debug bool // add additional log messages
|
||||
Verbose bool // add extra log message output
|
||||
}
|
||||
|
||||
// CLIArgs is a struct of values that we pass to the main CLI function.
|
||||
type CLIArgs struct {
|
||||
Program string
|
||||
@@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/purpleidea/mgmt/etcd/deployer"
|
||||
etcdfs "github.com/purpleidea/mgmt/etcd/fs"
|
||||
"github.com/purpleidea/mgmt/gapi"
|
||||
"github.com/purpleidea/mgmt/lib"
|
||||
"github.com/purpleidea/mgmt/util/errwrap"
|
||||
|
||||
"github.com/pborman/uuid"
|
||||
@@ -34,13 +35,6 @@ import (
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
)
|
||||
|
||||
const (
|
||||
// MetadataPrefix is the etcd prefix where all our fs superblocks live.
|
||||
MetadataPrefix = "/fs"
|
||||
// StoragePrefix is the etcd prefix where all our fs data lives.
|
||||
StoragePrefix = "/storage"
|
||||
)
|
||||
|
||||
// deploy is the cli target to manage deploys to our cluster.
|
||||
// TODO: add a timeout and/or cancel signal to replace context.TODO()
|
||||
func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
||||
@@ -111,7 +105,7 @@ func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
||||
|
||||
etcdClient := client.NewClientFromSeedsNamespace(
|
||||
cliContext.StringSlice("seeds"), // endpoints
|
||||
NS,
|
||||
lib.NS,
|
||||
)
|
||||
if err := etcdClient.Init(); err != nil {
|
||||
return errwrap.Wrapf(err, "client Init failed")
|
||||
@@ -154,8 +148,8 @@ func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
||||
etcdFs := &etcdfs.Fs{
|
||||
Client: etcdClient,
|
||||
// TODO: using a uuid is meant as a temporary measure, i hate them
|
||||
Metadata: MetadataPrefix + fmt.Sprintf("/deploy/%d-%s", id, uniqueid),
|
||||
DataPrefix: StoragePrefix,
|
||||
Metadata: lib.MetadataPrefix + fmt.Sprintf("/deploy/%d-%s", id, uniqueid),
|
||||
DataPrefix: lib.StoragePrefix,
|
||||
|
||||
Debug: debug,
|
||||
Logf: func(format string, v ...interface{}) {
|
||||
@@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"github.com/purpleidea/mgmt/gapi"
|
||||
"github.com/purpleidea/mgmt/lib"
|
||||
"github.com/purpleidea/mgmt/util"
|
||||
"github.com/purpleidea/mgmt/util/errwrap"
|
||||
|
||||
@@ -40,14 +41,25 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
||||
return fmt.Errorf("could not get cli context")
|
||||
}
|
||||
|
||||
obj := &Main{}
|
||||
obj := &lib.Main{}
|
||||
|
||||
obj.Program, obj.Version = safeProgram(c.App.Name), c.App.Version
|
||||
var flags Flags
|
||||
if val, exists := c.App.Metadata["flags"]; exists {
|
||||
if flags, ok := val.(Flags); ok {
|
||||
obj.Flags = flags
|
||||
if f, ok := val.(Flags); ok {
|
||||
flags = f
|
||||
obj.Flags = lib.Flags{
|
||||
Debug: f.Debug,
|
||||
Verbose: f.Verbose,
|
||||
}
|
||||
}
|
||||
}
|
||||
Logf := func(format string, v ...interface{}) {
|
||||
log.Printf("main: "+format, v...)
|
||||
}
|
||||
|
||||
hello(obj.Program, obj.Version, flags) // say hello!
|
||||
defer Logf("goodbye!")
|
||||
|
||||
if h := cliContext.String("hostname"); cliContext.IsSet("hostname") && h != "" {
|
||||
obj.Hostname = &h
|
||||
@@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
package cli
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -55,6 +55,12 @@ import (
|
||||
const (
|
||||
// NS is the root namespace for etcd operations. All keys must use it!
|
||||
NS = "/_mgmt" // must not end with a slash!
|
||||
|
||||
// MetadataPrefix is the etcd prefix where all our fs superblocks live.
|
||||
MetadataPrefix = "/fs"
|
||||
|
||||
// StoragePrefix is the etcd prefix where all our fs data lives.
|
||||
StoragePrefix = "/storage"
|
||||
)
|
||||
|
||||
// Flags are some constant flags which are used throughout the program.
|
||||
@@ -202,9 +208,6 @@ func (obj *Main) Run() error {
|
||||
log.Printf("main: "+format, v...)
|
||||
}
|
||||
|
||||
hello(obj.Program, obj.Version, obj.Flags) // say hello!
|
||||
defer Logf("goodbye!")
|
||||
|
||||
exitCtx := obj.exit.Context() // local exit signal
|
||||
defer obj.exit.Done(nil) // ensure this gets called even if Exit doesn't
|
||||
|
||||
|
||||
15
main.go
15
main.go
@@ -22,7 +22,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
mgmt "github.com/purpleidea/mgmt/lib"
|
||||
"github.com/purpleidea/mgmt/cli"
|
||||
"go.etcd.io/etcd/server/v3/etcdmain"
|
||||
)
|
||||
|
||||
@@ -55,17 +55,16 @@ func main() {
|
||||
return // for safety
|
||||
}
|
||||
|
||||
flags := mgmt.Flags{
|
||||
Debug: Debug,
|
||||
Verbose: Verbose,
|
||||
}
|
||||
cliArgs := &mgmt.CLIArgs{
|
||||
cliArgs := &cli.CLIArgs{
|
||||
Program: program,
|
||||
Version: version,
|
||||
Copying: copying,
|
||||
Flags: flags,
|
||||
Flags: cli.Flags{
|
||||
Debug: Debug,
|
||||
Verbose: Verbose,
|
||||
},
|
||||
}
|
||||
if err := mgmt.CLI(cliArgs); err != nil {
|
||||
if err := cli.CLI(cliArgs); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user