diff --git a/cli/server/add.go b/cli/server/add.go index bee7c339f..b290e5d1a 100644 --- a/cli/server/add.go +++ b/cli/server/add.go @@ -3,14 +3,12 @@ package server import ( "context" "errors" - "os" "os/user" - "path" "strings" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/client" - "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/server" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -68,7 +66,10 @@ All communication between Abra and the server will use this SSH connection. domainName := "default" if local { - os.Mkdir(path.Join(config.ABRA_DIR, "servers", domainName), 0755) + if err := server.CreateServerDir(domainName); err != nil { + logrus.Fatal(err) + } + logrus.Info("local server has been added") return nil } @@ -125,9 +126,12 @@ All communication between Abra and the server will use this SSH connection. } logrus.Debugf("remote connection to '%s' is definitely up", domainName) - logrus.Infof("server at '%s' has been added", domainName) - os.Mkdir(path.Join(config.ABRA_DIR, "servers", domainName), 0755) + if err := server.CreateServerDir(domainName); err != nil { + logrus.Fatal(err) + } + + logrus.Infof("server at '%s' has been added", domainName) return nil }, diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 000000000..5a9452e6b --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,24 @@ +package server + +import ( + "os" + "path" + + "coopcloud.tech/abra/pkg/config" + "github.com/sirupsen/logrus" +) + +// CreateServerDir creates a server directory under ~/.abra. +func CreateServerDir(serverName string) error { + serverPath := path.Join(config.ABRA_DIR, "servers", serverName) + + if err := os.Mkdir(serverPath, 0755); err != nil { + if !os.IsExist(err) { + return err + } + + logrus.Infof("'%s' already exists, moving on...", serverPath) + } + + return nil +}