Skip to content

Commit

Permalink
Make meta-repo a persistent flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrull committed Jul 24, 2024
1 parent 281afd7 commit 60da0bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
19 changes: 17 additions & 2 deletions src/go/cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"
)
Expand All @@ -13,7 +15,7 @@ var (
)

// Configure the root command with the given I/O and version identifier, then return for use.
func NewRootCommand(stdout io.Writer, stderr io.Writer, version string) *cobra.Command {
func NewRootCommand(stdout io.Writer, stderr io.Writer, version string) (*cobra.Command, error) {
rootCmd = &cobra.Command{
Long: "marmot manages a Meta Repository that organizes content in other (Git) repositories.",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -34,14 +36,27 @@ func NewRootCommand(stdout io.Writer, stderr io.Writer, version string) *cobra.C
// Flags
debugFlag = rootCmd.PersistentFlags().Bool("debug", false, "print CLI debugging information")
rootCmd.PersistentFlags().Lookup("debug").Hidden = true
if defaultPath, pathErr := defaultMetaRepoPath(); pathErr != nil {
return nil, pathErr
} else {
rootCmd.PersistentFlags().String("meta-repo", defaultPath, "Meta repo to use")
}

// Groups
rootCmd.AddGroup(&cobra.Group{ID: metaRepoGroup, Title: "Meta Repo Commands"})

// I/O
rootCmd.SetOut(stdout)
rootCmd.SetErr(stderr)
return rootCmd
return rootCmd, nil
}

func defaultMetaRepoPath() (string, error) {
if homeDir, homeErr := os.UserHomeDir(); homeErr != nil {
return "", fmt.Errorf("failed to locate home directory; %w", homeErr)
} else {
return filepath.Join(homeDir, "meta"), nil
}
}

/* Child commands */
Expand Down
31 changes: 5 additions & 26 deletions src/go/cmdinit/init_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cmdinit

import (
"fmt"
"os"
"path/filepath"

"github.com/kkrull/marmot/cmd"
"github.com/spf13/cobra"
Expand All @@ -16,17 +14,13 @@ func NewInitCommand() *initCommand {

type initCommand struct{}

func (cliCmd *initCommand) RegisterWithCobra() error {
if cobraCmd, cobraErr := cliCmd.toCobraCommand(); cobraErr != nil {
return cobraErr
} else {
cmd.AddMetaRepoCommand(*cobraCmd)
return nil
}
func (cliCmd *initCommand) RegisterWithCobra() {
cobraCmd := cliCmd.toCobraCommand()
cmd.AddMetaRepoCommand(*cobraCmd)
}

func (cliCmd *initCommand) toCobraCommand() (*cobra.Command, error) {
cobraCmd := &cobra.Command{
func (cliCmd *initCommand) toCobraCommand() *cobra.Command {
return &cobra.Command{
Long: "Initialize a new Meta Repo, if none is already present.",
RunE: func(cobraCmd *cobra.Command, _args []string) error {
config := cmd.ParseFlags(cobraCmd)
Expand All @@ -42,19 +36,4 @@ func (cliCmd *initCommand) toCobraCommand() (*cobra.Command, error) {
Short: "Initialize a meta repo",
Use: "init",
}

if defaultPath, pathErr := defaultMetaRepoPath(); pathErr != nil {
return nil, pathErr
} else {
cobraCmd.Flags().String("meta-repo", defaultPath, "Meta repo to use")
return cobraCmd, nil
}
}

func defaultMetaRepoPath() (string, error) {
if homeDir, homeErr := os.UserHomeDir(); homeErr != nil {
return "", fmt.Errorf("failed to locate home directory; %w", homeErr)
} else {
return filepath.Join(homeDir, "meta"), nil
}
}
13 changes: 8 additions & 5 deletions src/go/mainfactory/cli_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ func (cliFactory *CliFactory) WithStdIO(stdout io.Writer, stderr io.Writer) *Cli
/* Factory methods */

func (cliFactory *CliFactory) CommandTree() (*cobra.Command, error) {
rootCmd := cmd.NewRootCommand(cliFactory.stdout, cliFactory.stderr, cliFactory.version)
cmdinit.
NewInitCommand().
RegisterWithCobra()
return rootCmd, nil
if rootCmd, rootCmdErr := cmd.NewRootCommand(cliFactory.stdout, cliFactory.stderr, cliFactory.version); rootCmdErr != nil {
return nil, rootCmdErr
} else {
cmdinit.
NewInitCommand().
RegisterWithCobra()
return rootCmd, nil
}
}

/* Version configuration */
Expand Down

0 comments on commit 60da0bc

Please sign in to comment.