diff --git a/lib/cli.go b/cli/cli.go
similarity index 96%
rename from lib/cli.go
rename to cli/cli.go
index 33262ed2..814bca0e 100644
--- a/lib/cli.go
+++ b/cli/cli.go
@@ -15,7 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-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
diff --git a/lib/deploy.go b/cli/deploy.go
similarity index 94%
rename from lib/deploy.go
rename to cli/deploy.go
index 8cdc8fd1..c86faf68 100644
--- a/lib/deploy.go
+++ b/cli/deploy.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-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{}) {
diff --git a/lib/get.go b/cli/get.go
similarity index 99%
rename from lib/get.go
rename to cli/get.go
index 9bce6414..5d2c9405 100644
--- a/lib/get.go
+++ b/cli/get.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-package lib
+package cli
import (
"fmt"
diff --git a/lib/hello.go b/cli/hello.go
similarity index 99%
rename from lib/hello.go
rename to cli/hello.go
index aaa01ba5..ff1f6f1d 100644
--- a/lib/hello.go
+++ b/cli/hello.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-package lib
+package cli
import (
"fmt"
diff --git a/lib/run.go b/cli/run.go
similarity index 93%
rename from lib/run.go
rename to cli/run.go
index 1cff95a5..40478af8 100644
--- a/lib/run.go
+++ b/cli/run.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-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
diff --git a/lib/util.go b/cli/util.go
similarity index 99%
rename from lib/util.go
rename to cli/util.go
index fb1b2c39..7289c848 100644
--- a/lib/util.go
+++ b/cli/util.go
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-package lib
+package cli
import (
"strings"
diff --git a/lib/main.go b/lib/main.go
index 7776b48f..882e100e 100644
--- a/lib/main.go
+++ b/lib/main.go
@@ -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
diff --git a/main.go b/main.go
index 676c4cb6..d4ae1339 100644
--- a/main.go
+++ b/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