fix: make server path creation more robust

This commit is contained in:
decentral1se 2021-10-02 22:30:08 +02:00
parent db5cbfa992
commit 48290aa316
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 34 additions and 6 deletions

View File

@ -3,14 +3,12 @@ package server
import ( import (
"context" "context"
"errors" "errors"
"os"
"os/user" "os/user"
"path"
"strings" "strings"
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/server"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -68,7 +66,10 @@ All communication between Abra and the server will use this SSH connection.
domainName := "default" domainName := "default"
if local { 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 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.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 return nil
}, },

24
pkg/server/server.go Normal file
View File

@ -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
}