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
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// 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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -32,6 +35,12 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"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.
|
// CLIArgs is a struct of values that we pass to the main CLI function.
|
||||||
type CLIArgs struct {
|
type CLIArgs struct {
|
||||||
Program string
|
Program string
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/purpleidea/mgmt/etcd/deployer"
|
"github.com/purpleidea/mgmt/etcd/deployer"
|
||||||
etcdfs "github.com/purpleidea/mgmt/etcd/fs"
|
etcdfs "github.com/purpleidea/mgmt/etcd/fs"
|
||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
"github.com/purpleidea/mgmt/lib"
|
||||||
"github.com/purpleidea/mgmt/util/errwrap"
|
"github.com/purpleidea/mgmt/util/errwrap"
|
||||||
|
|
||||||
"github.com/pborman/uuid"
|
"github.com/pborman/uuid"
|
||||||
@@ -34,13 +35,6 @@ import (
|
|||||||
git "gopkg.in/src-d/go-git.v4"
|
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.
|
// deploy is the cli target to manage deploys to our cluster.
|
||||||
// TODO: add a timeout and/or cancel signal to replace context.TODO()
|
// TODO: add a timeout and/or cancel signal to replace context.TODO()
|
||||||
func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
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(
|
etcdClient := client.NewClientFromSeedsNamespace(
|
||||||
cliContext.StringSlice("seeds"), // endpoints
|
cliContext.StringSlice("seeds"), // endpoints
|
||||||
NS,
|
lib.NS,
|
||||||
)
|
)
|
||||||
if err := etcdClient.Init(); err != nil {
|
if err := etcdClient.Init(); err != nil {
|
||||||
return errwrap.Wrapf(err, "client Init failed")
|
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{
|
etcdFs := &etcdfs.Fs{
|
||||||
Client: etcdClient,
|
Client: etcdClient,
|
||||||
// TODO: using a uuid is meant as a temporary measure, i hate them
|
// TODO: using a uuid is meant as a temporary measure, i hate them
|
||||||
Metadata: MetadataPrefix + fmt.Sprintf("/deploy/%d-%s", id, uniqueid),
|
Metadata: lib.MetadataPrefix + fmt.Sprintf("/deploy/%d-%s", id, uniqueid),
|
||||||
DataPrefix: StoragePrefix,
|
DataPrefix: lib.StoragePrefix,
|
||||||
|
|
||||||
Debug: debug,
|
Debug: debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
"github.com/purpleidea/mgmt/lib"
|
||||||
"github.com/purpleidea/mgmt/util"
|
"github.com/purpleidea/mgmt/util"
|
||||||
"github.com/purpleidea/mgmt/util/errwrap"
|
"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")
|
return fmt.Errorf("could not get cli context")
|
||||||
}
|
}
|
||||||
|
|
||||||
obj := &Main{}
|
obj := &lib.Main{}
|
||||||
|
|
||||||
obj.Program, obj.Version = safeProgram(c.App.Name), c.App.Version
|
obj.Program, obj.Version = safeProgram(c.App.Name), c.App.Version
|
||||||
|
var flags Flags
|
||||||
if val, exists := c.App.Metadata["flags"]; exists {
|
if val, exists := c.App.Metadata["flags"]; exists {
|
||||||
if flags, ok := val.(Flags); ok {
|
if f, ok := val.(Flags); ok {
|
||||||
obj.Flags = flags
|
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 != "" {
|
if h := cliContext.String("hostname"); cliContext.IsSet("hostname") && h != "" {
|
||||||
obj.Hostname = &h
|
obj.Hostname = &h
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -55,6 +55,12 @@ import (
|
|||||||
const (
|
const (
|
||||||
// NS is the root namespace for etcd operations. All keys must use it!
|
// NS is the root namespace for etcd operations. All keys must use it!
|
||||||
NS = "/_mgmt" // must not end with a slash!
|
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.
|
// 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...)
|
log.Printf("main: "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
hello(obj.Program, obj.Version, obj.Flags) // say hello!
|
|
||||||
defer Logf("goodbye!")
|
|
||||||
|
|
||||||
exitCtx := obj.exit.Context() // local exit signal
|
exitCtx := obj.exit.Context() // local exit signal
|
||||||
defer obj.exit.Done(nil) // ensure this gets called even if Exit doesn't
|
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"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
mgmt "github.com/purpleidea/mgmt/lib"
|
"github.com/purpleidea/mgmt/cli"
|
||||||
"go.etcd.io/etcd/server/v3/etcdmain"
|
"go.etcd.io/etcd/server/v3/etcdmain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,17 +55,16 @@ func main() {
|
|||||||
return // for safety
|
return // for safety
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := mgmt.Flags{
|
cliArgs := &cli.CLIArgs{
|
||||||
Debug: Debug,
|
|
||||||
Verbose: Verbose,
|
|
||||||
}
|
|
||||||
cliArgs := &mgmt.CLIArgs{
|
|
||||||
Program: program,
|
Program: program,
|
||||||
Version: version,
|
Version: version,
|
||||||
Copying: copying,
|
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)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user