Skip to content

Commit

Permalink
lib: Split off the CLI portions into a separate package
Browse files Browse the repository at this point in the history
This cleans things up a bit more and forces the lib package to not
contain any accidental CLI parsing code.
  • Loading branch information
purpleidea committed Feb 28, 2024
1 parent 70b5ed7 commit abe3e0e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 30 deletions.
11 changes: 10 additions & 1 deletion lib/cli.go → cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
16 changes: 5 additions & 11 deletions lib/deploy.go → cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,20 +27,14 @@ 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"
"github.com/urfave/cli/v2"
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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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{}) {
Expand Down
2 changes: 1 addition & 1 deletion lib/get.go → cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/hello.go → cli/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 16 additions & 4 deletions lib/run.go → cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/util.go → cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 6 additions & 3 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit abe3e0e

Please sign in to comment.