Skip to content

Commit

Permalink
Review logging and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
geaaru committed May 23, 2024
1 parent beb4970 commit ba192cb
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 130 deletions.
24 changes: 11 additions & 13 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func newApplyCommand(config *specs.SshComposeConfig) *cobra.Command {
os.Exit(1)
}

logger := composer.GetLogger()

// We need set this before loading phase
err = config.SetRenderEnvs(renderEnvs)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
logger.Fatal("Error on render specs: " + err.Error() + "\n")
}

err = composer.LoadEnvironments()
if err != nil {
fmt.Println("Error on load environments:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on load environments:" + err.Error() + "\n")
}

skipSync, _ := cmd.Flags().GetBool("skip-sync")
Expand All @@ -67,22 +67,21 @@ func newApplyCommand(config *specs.SshComposeConfig) *cobra.Command {

for _, proj := range projects {

fmt.Println("Apply project " + proj)
logger.InfoC(
logger.Aurora.Bold(fmt.Sprintf(">>> Applying project :right_arrow:%s :rocket:", proj)))

env := composer.GetEnvByProjectName(proj)
if env == nil {
fmt.Println("Project " + proj + " not found")
os.Exit(1)
logger.Fatal("Project " + proj + " not found")
}

pObj := env.GetProjectByName(proj)
for _, varFile := range varsFiles {
err := pObj.LoadEnvVarsFile(varFile)
if err != nil {
fmt.Println(fmt.Sprintf(
logger.Fatal(fmt.Sprintf(
"Error on load additional envs var file %s: %s",
varFile, err.Error()))
os.Exit(1)
}
}

Expand All @@ -92,8 +91,7 @@ func newApplyCommand(config *specs.SshComposeConfig) *cobra.Command {
for _, e := range envs {
err := evars.AddKVAggregated(e)
if err != nil {
fmt.Println(err)
os.Exit(1)
logger.Fatal(err.Error())
}
}

Expand All @@ -102,13 +100,13 @@ func newApplyCommand(config *specs.SshComposeConfig) *cobra.Command {

err = composer.ApplyProject(proj)
if err != nil {
fmt.Println("Error on apply project " + proj + ": " + err.Error())
logger.Fatal("Error on apply project " + proj + ": " + err.Error())
os.Exit(1)
}

}

fmt.Println("All done.")
logger.InfoC(":tada:All done!")
},
}

Expand Down
42 changes: 24 additions & 18 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

ssh_executor "github.com/MottainaiCI/ssh-compose/pkg/executor"
loader "github.com/MottainaiCI/ssh-compose/pkg/loader"
specs "github.com/MottainaiCI/ssh-compose/pkg/specs"

"github.com/spf13/cobra"
Expand All @@ -28,60 +29,60 @@ func NewExecCommand(config *specs.SshComposeConfig) *cobra.Command {

remoteName := args[0]

remotes, err := specs.LoadRemotesConfig(
config.GetGeneral().RemotesConfDir,
)
// Create Instance also if not really used but
// contains the right setup of the logger and the
// load of the remotes.
composer, err := loader.NewSshCInstance(config)
if err != nil {
fmt.Println("Error:", err.Error())
fmt.Println("error on setup instance", err.Error())
os.Exit(1)
}

remotes := composer.GetRemotes()
logger := composer.GetLogger()

if !remotes.HasRemote(remoteName) {
fmt.Println(fmt.Sprintf("Remote %s not found.", remoteName))
os.Exit(1)
logger.Fatal(fmt.Sprintf("Remote %s not found.", remoteName))
}
remote := remotes.GetRemote(remoteName)

executor, err := ssh_executor.NewSshCExecutorFromRemote(remoteName, remote)
if err != nil {
fmt.Println("Error on create executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on create executor:" + err.Error() + "\n")
}
err = executor.Setup()
if err != nil {
fmt.Println("Error on setup executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on setup executor:" + err.Error() + "\n")
}
defer executor.Close()

session, err := executor.GetSession("my-session")
if err != nil {
fmt.Println("Error on get session:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on get session:" + err.Error() + "\n")
}

if !withoutEnvs {
err = session.Setenv(fmt.Sprintf("%s_VERSION",
config.GetGeneral().EnvSessionPrefix),
specs.SSH_COMPOSE_VERSION)
if err != nil {
fmt.Println("ERR on set env", err.Error())
logger.Debug("Error on set version env", err.Error())
}

if len(envs) > 0 {
for _, env := range envs {
e := specs.NewEnvVars()
err := e.AddKVAggregated(env)
if err != nil {
fmt.Println(fmt.Sprintf(
logger.Debug(fmt.Sprintf(
"Invalid env variable %s: %s", env, err.Error()))
continue
}

for k, v := range e.EnvVars {
err = session.Setenv(k, v.(string))
if err != nil {
fmt.Println(fmt.Sprintf(
logger.Debug(fmt.Sprintf(
"error on set env variable %s: %s", env, err.Error()))
continue
}
Expand All @@ -98,11 +99,16 @@ func NewExecCommand(config *specs.SshComposeConfig) *cobra.Command {

runArgs := strings.Join(args[1:], " ")

fmt.Println("Running: ", runArgs)
logger.InfoC(
logger.Aurora.Italic(
logger.Aurora.BrightCyan(
fmt.Sprintf(">>> [%s] - %s - :coffee:",
remoteName, runArgs,
))))

err = session.Run(runArgs)
if err != nil {
fmt.Println("error on execute command ", err.Error())
os.Exit(1)
logger.Fatal("error on execute command ", err.Error())
}
},
}
Expand Down
15 changes: 5 additions & 10 deletions cmd/file/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ func NewPullCommand(config *specs.SshComposeConfig) *cobra.Command {
logger := composer.GetLogger()

if !remotes.HasRemote(remoteName) {
fmt.Println(fmt.Sprintf("Remote %s not found.", remoteName))
os.Exit(1)
logger.Fatal(fmt.Sprintf("Remote %s not found.", remoteName))
}
remote := remotes.GetRemote(remoteName)

executor, err := ssh_executor.NewSshCExecutorFromRemote(remoteName, remote)
if err != nil {
fmt.Println("Error on create executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on create executor:" + err.Error() + "\n")
}
err = executor.Setup()
if err != nil {
fmt.Println("Error on setup executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on setup executor:" + err.Error() + "\n")
}
defer executor.Close()

Expand All @@ -68,9 +65,8 @@ func NewPullCommand(config *specs.SshComposeConfig) *cobra.Command {

err = executor.SetupSftp()
if err != nil {
fmt.Println("Error on setup sftp client on executor of the node " +
logger.Fatal("Error on setup sftp client on executor of the node " +
remoteName + ": " + err.Error())
os.Exit(1)
}

logger.InfoC(
Expand All @@ -83,9 +79,8 @@ func NewPullCommand(config *specs.SshComposeConfig) *cobra.Command {
err = executor.RecursivePullFile(remoteName,
remotePath, localPath, localAsTarget, ensurePerms)
if err != nil {
fmt.Println("Error on pull files from " +
logger.Fatal("Error on pull files from " +
remoteName + ": " + err.Error())
os.Exit(1)
}

logger.InfoC(":tada:All done!")
Expand Down
15 changes: 5 additions & 10 deletions cmd/file/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ func NewPushCommand(config *specs.SshComposeConfig) *cobra.Command {
logger := composer.GetLogger()

if !remotes.HasRemote(remoteName) {
fmt.Println(fmt.Sprintf("Remote %s not found.", remoteName))
os.Exit(1)
logger.Fatal(fmt.Sprintf("Remote %s not found.", remoteName))
}
remote := remotes.GetRemote(remoteName)

executor, err := ssh_executor.NewSshCExecutorFromRemote(remoteName, remote)
if err != nil {
fmt.Println("Error on create executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on create executor:" + err.Error() + "\n")
}
err = executor.Setup()
if err != nil {
fmt.Println("Error on setup executor:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on setup executor:" + err.Error() + "\n")
}
defer executor.Close()

Expand All @@ -67,9 +64,8 @@ func NewPushCommand(config *specs.SshComposeConfig) *cobra.Command {

err = executor.SetupSftp()
if err != nil {
fmt.Println("Error on setup sftp client on executor of the node " +
logger.Fatal("Error on setup sftp client on executor of the node " +
remoteName + ": " + err.Error())
os.Exit(1)
}

logger.InfoC(
Expand All @@ -82,9 +78,8 @@ func NewPushCommand(config *specs.SshComposeConfig) *cobra.Command {
err = executor.RecursivePushFile(remoteName,
localPath, remotePath, ensurePerms)
if err != nil {
fmt.Println("Error on push files to " +
logger.Fatal("Error on push files to " +
remoteName + ": " + err.Error())
os.Exit(1)
}

logger.InfoC(":tada:All done!")
Expand Down
8 changes: 4 additions & 4 deletions cmd/group/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ func NewListCommand(config *specs.SshComposeConfig) *cobra.Command {
os.Exit(1)
}

logger := composer.GetLogger()

err = composer.LoadEnvironments()
if err != nil {
fmt.Println("Error on load environments:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on load environments:" + err.Error() + "\n")
}

project := args[0]
env := composer.GetEnvByProjectName(project)
if env == nil {
fmt.Println("Project not found")
os.Exit(1)
logger.Fatal("Project not found")
}

proj := env.GetProjectByName(project)
Expand Down
9 changes: 5 additions & 4 deletions cmd/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func NewListCommand(config *specs.SshComposeConfig) *cobra.Command {
fmt.Println("Error on setup sshc instance:" + err.Error() + "\n")
os.Exit(1)
}

logger := composer.GetLogger()

err = composer.LoadEnvironments()
if err != nil {
fmt.Println("Error on load environments:" + err.Error() + "\n")
os.Exit(1)
logger.Fatal("Error on load environments:" + err.Error() + "\n")
}

for _, e := range *composer.GetEnvironments() {
Expand All @@ -57,8 +59,7 @@ func NewListCommand(config *specs.SshComposeConfig) *cobra.Command {

data, err := json.Marshal(projects)
if err != nil {
fmt.Println("Error on decode projects ", err.Error())
os.Exit(1)
logger.Fatal("Error on decode projects ", err.Error())
}
fmt.Println(string(data))

Expand Down
Loading

0 comments on commit ba192cb

Please sign in to comment.