Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions foreman-builder/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
}
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -92,12 +87,11 @@ func createOrbstackContainer(opts foremanbuilder.OrbOptions) error {
}

// check for errors by doing ssh <container-name>@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
Expand Down
12 changes: 3 additions & 9 deletions foreman-builder/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"

Expand All @@ -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)
Expand Down
14 changes: 3 additions & 11 deletions foreman-builder/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cmd

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

foremanbuilder "github.com/aidenfine/foreman-builder/foreman-builder"
"github.com/spf13/cobra"
)
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions foreman-builder/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

foremanbuilder "github.com/aidenfine/foreman-builder/foreman-builder"
"github.com/spf13/cobra"
)

Expand All @@ -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)
Expand All @@ -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)
}
13 changes: 13 additions & 0 deletions foreman-builder/user.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading