diff --git a/foreman-builder/cmd/create.go b/foreman-builder/cmd/create.go index 429f2bb..647ddde 100644 --- a/foreman-builder/cmd/create.go +++ b/foreman-builder/cmd/create.go @@ -17,13 +17,13 @@ var createCmd = &cobra.Command{ Use: "create", Short: "Create a new container environment", Run: func(cmd *cobra.Command, args []string) { - runCreate() + foremanUser.runCreate() }, } var containerType = "orb" -func runCreate() { +func (u User) runCreate() { currentUser, err := user.Current() if err != nil { foremanbuilder.Logger.Fatalf("Failed to get current user: %s", err) @@ -39,12 +39,7 @@ func runCreate() { containerName = "foreman" } - home, err := os.UserHomeDir() - if err != nil { - foremanbuilder.Logger.Fatalf("Failed to get home directory: %v", err) - } - - containerNameExists, err := foremanbuilder.GetLineInFile(filepath.Join(home, ".foreman-builder/containers"), containerName, "") + containerNameExists, err := foremanbuilder.GetLineInFile(u.containersPath, containerName, "") if err != nil { if err.Error() != "not_found" { fmt.Println("An Error has occured searching dotfile") @@ -58,7 +53,7 @@ func runCreate() { os.Exit(1) } - err = foremanbuilder.AppendToFile(filepath.Join(home, ".foreman-builder/containers"), fmt.Sprintf("%s::%s", containerName, containerType)) + err = foremanbuilder.AppendToFile(u.containersPath, fmt.Sprintf("%s::%s", containerName, containerType)) if err != nil { foremanbuilder.Logger.Error("Failed to write container to container file") } @@ -70,7 +65,7 @@ func runCreate() { Username: username, ContainerName: containerName, } - err := createOrbstackContainer(orbOpts) + err := foremanUser.createOrbstackContainer(orbOpts) if err != nil { // better error message to show? fmt.Println("An error has occured during container creation") @@ -80,7 +75,7 @@ func runCreate() { } -func createOrbstackContainer(opts foremanbuilder.OrbOptions) error { +func (u User) createOrbstackContainer(opts foremanbuilder.OrbOptions) error { config, err := foremanbuilder.GetYmlValues("./config.yml") if err != nil { foremanbuilder.Logger.Info("No config file found, skipping") @@ -92,12 +87,11 @@ func createOrbstackContainer(opts foremanbuilder.OrbOptions) error { } // check for errors by doing ssh @orb cat /var/log/cloud-init-output.log - home, err := os.UserHomeDir() if err != nil { foremanbuilder.Logger.Errorf("Failed to get home directory: %v", err) return err } - confsDir := filepath.Join(home, ".foreman-builder", "confs") + confsDir := filepath.Join(u.dotFilePath, "confs") if err := os.MkdirAll(confsDir, 0755); err != nil { foremanbuilder.Logger.Errorf("Failed to create confs directory: %v", err) return err diff --git a/foreman-builder/cmd/delete.go b/foreman-builder/cmd/delete.go index 96782c5..a5f28df 100644 --- a/foreman-builder/cmd/delete.go +++ b/foreman-builder/cmd/delete.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "slices" "strings" @@ -22,20 +21,15 @@ var deleteCmd = &cobra.Command{ os.Exit(1) } containerName := args[0] - runDelete(fmt.Sprint(strings.Split(containerName, "::")[0])) + foremanUser.runDelete(fmt.Sprint(strings.Split(containerName, "::")[0])) }, } -func runDelete(containerName string) { +func (u User) runDelete(containerName string) { // check if item actually exists first AND foreman-builder created it - home, err := os.UserHomeDir() - if err != nil { - foremanbuilder.Logger.Fatalf("Failed to get home directory: %v", err) - } - dotFolderPath := filepath.Join(home, ".foreman-builder") - containersPath := filepath.Join(dotFolderPath, "containers") + containersPath := u.containersPath containers, err := foremanbuilder.GetAllLines(containersPath, "::") foremanbuilder.Logger.Debugf("containers: %v\n", containers) foremanbuilder.Logger.Debugf("containerName: %s \n", containerName) diff --git a/foreman-builder/cmd/list.go b/foreman-builder/cmd/list.go index 562a8f9..ba166cf 100644 --- a/foreman-builder/cmd/list.go +++ b/foreman-builder/cmd/list.go @@ -2,9 +2,6 @@ package cmd import ( "fmt" - "os" - "path/filepath" - foremanbuilder "github.com/aidenfine/foreman-builder/foreman-builder" "github.com/spf13/cobra" ) @@ -14,18 +11,13 @@ var listCmd = &cobra.Command{ Short: "List all containers created with foreman-builder.", Long: "List all containers created with foreman-builder.", Run: func(cmd *cobra.Command, args []string) { - runList() + foremanUser.runList() }, } -func runList() { +func(u User) runList() { fmt.Println("Foreman containers:") - home, err := os.UserHomeDir() - if err != nil { - foremanbuilder.Logger.Fatalf("Failed to get home directory: %v", err) - } - dotFolderPath := filepath.Join(home, ".foreman-builder") - containersPath := filepath.Join(dotFolderPath, "containers") + containersPath := u.containersPath containers, err := foremanbuilder.GetAllLines(containersPath, "::") if err != nil { fmt.Printf("Error getting all containers %v\n", err) diff --git a/foreman-builder/cmd/root.go b/foreman-builder/cmd/root.go index f490e45..342ebf1 100644 --- a/foreman-builder/cmd/root.go +++ b/foreman-builder/cmd/root.go @@ -3,7 +3,9 @@ package cmd import ( "fmt" "os" + "path/filepath" + foremanbuilder "github.com/aidenfine/foreman-builder/foreman-builder" "github.com/spf13/cobra" ) @@ -13,6 +15,14 @@ var rootCmd = &cobra.Command{ Long: "A CLI tool to create and manage foreman containers.", } +type User struct { + homeDir string + dotFilePath string + containersPath string +} + +var foremanUser User + func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) @@ -21,8 +31,19 @@ func Execute() { } func init() { + foremanUser = User{ + homeDir: foremanbuilder.GetHomeDir(), + dotFilePath: "", + containersPath: "", + } + foremanUser.dotFilePath = filepath.Join(foremanUser.homeDir, ".foreman-builder") + if foremanUser.dotFilePath != "" { + foremanUser.containersPath = filepath.Join(foremanUser.dotFilePath, "containers") + } + rootCmd.AddCommand(createCmd) rootCmd.AddCommand(listCmd) rootCmd.AddCommand(deleteCmd) + // rootCmd.AddCommand(syncCmd) } diff --git a/foreman-builder/user.go b/foreman-builder/user.go new file mode 100644 index 0000000..47fad1a --- /dev/null +++ b/foreman-builder/user.go @@ -0,0 +1,13 @@ +package foremanbuilder + +import ( + "os" +) + +func GetHomeDir() string { + home, err := os.UserHomeDir() + if err != nil { + Logger.Fatalf("Failed to get home directory: %v", err) + } + return home +}