Move util.CheckError to common.CheckError

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-03 09:51:11 -04:00
parent 3989715b70
commit 356979a7a4
8 changed files with 54 additions and 42 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"github.com/greenled/portainer-stack-utils/common"
"github.com/greenled/portainer-stack-utils/util"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
@ -33,12 +34,12 @@ var configCmd = &cobra.Command{
if len(args) == 1 {
// Get config
value, configGettingErr := getConfig(args[0])
util.CheckError(configGettingErr)
common.CheckError(configGettingErr)
fmt.Println(value)
} else {
// Set config
configSettingErr := setConfig(args[0], args[1])
util.CheckError(configSettingErr)
common.CheckError(configSettingErr)
}
},
}

View File

@ -36,18 +36,18 @@ var endpointListCmd = &cobra.Command{
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
client, err := common.GetClient()
util.CheckError(err)
common.CheckError(err)
endpoints, err := client.GetEndpoints()
util.CheckError(err)
common.CheckError(err)
if viper.GetString("endpoint.list.format") != "" {
// Print endpoint fields formatted
template, templateParsingErr := template.New("endpointTpl").Parse(viper.GetString("endpoint.list.format"))
util.CheckError(templateParsingErr)
common.CheckError(templateParsingErr)
for _, e := range endpoints {
templateExecutionErr := template.Execute(os.Stdout, e)
util.CheckError(templateExecutionErr)
common.CheckError(templateExecutionErr)
fmt.Println()
}
} else {
@ -60,7 +60,7 @@ var endpointListCmd = &cobra.Command{
"PUBLIC URL",
"GROUP ID",
})
util.CheckError(err)
common.CheckError(err)
for _, e := range endpoints {
var endpointType string
if e.Type == 1 {
@ -77,10 +77,10 @@ var endpointListCmd = &cobra.Command{
e.PublicURL,
e.GroupID,
))
util.CheckError(err)
common.CheckError(err)
}
flushErr := writer.Flush()
util.CheckError(flushErr)
common.CheckError(flushErr)
}
},
}

View File

@ -3,8 +3,6 @@ package cmd
import (
"fmt"
"github.com/greenled/portainer-stack-utils/util"
"github.com/greenled/portainer-stack-utils/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -17,10 +15,10 @@ var loginCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// Get auth token
client, err := common.GetDefaultClient()
util.CheckError(err)
common.CheckError(err)
authToken, err := client.Authenticate()
util.CheckError(err)
common.CheckError(err)
if viper.GetBool("login.print") {
fmt.Println(authToken)
@ -28,7 +26,7 @@ var loginCmd = &cobra.Command{
// Save auth token
configSettingErr := setConfig("auth-token", authToken)
util.CheckError(configSettingErr)
common.CheckError(configSettingErr)
},
}

View File

@ -27,11 +27,11 @@ var stackDeployCmd = &cobra.Command{
if viper.GetString("stack.deploy.env-file") != "" {
var loadingErr error
loadedEnvironmentVariables, loadingErr = loadEnvironmentVariablesFile(viper.GetString("stack.deploy.env-file"))
util.CheckError(loadingErr)
common.CheckError(loadingErr)
}
portainerClient, clientRetrievalErr := common.GetClient()
util.CheckError(clientRetrievalErr)
common.CheckError(clientRetrievalErr)
stackName := args[0]
retrievedStack, stackRetrievalErr := common.GetStackByName(stackName)
@ -44,11 +44,11 @@ var stackDeployCmd = &cobra.Command{
if viper.GetString("stack.deploy.stack-file") != "" {
var loadingErr error
stackFileContent, loadingErr = loadStackFile(viper.GetString("stack.deploy.stack-file"))
util.CheckError(loadingErr)
common.CheckError(loadingErr)
} else {
var stackFileContentRetrievalErr error
stackFileContent, stackFileContentRetrievalErr = portainerClient.GetStackFileContent(retrievedStack.Id)
util.CheckError(stackFileContentRetrievalErr)
common.CheckError(stackFileContentRetrievalErr)
}
var newEnvironmentVariables []client.StackEnv
@ -73,7 +73,7 @@ var stackDeployCmd = &cobra.Command{
}
err := portainerClient.UpdateStack(retrievedStack, newEnvironmentVariables, stackFileContent, viper.GetBool("stack.deploy.prune"), viper.GetString("stack.deploy.endpoint"))
util.CheckError(err)
common.CheckError(err)
case *common.StackNotFoundError:
// We are deploying a new stack
util.PrintVerbose(fmt.Sprintf("Stack %s not found. Deploying...", stackName))
@ -82,7 +82,7 @@ var stackDeployCmd = &cobra.Command{
log.Fatalln("Specify a docker-compose file with --stack-file")
}
stackFileContent, loadingErr := loadStackFile(viper.GetString("stack.deploy.stack-file"))
util.CheckError(loadingErr)
common.CheckError(loadingErr)
swarmClusterId, selectionErr := getSwarmClusterId()
switch selectionErr.(type) {
@ -90,19 +90,19 @@ var stackDeployCmd = &cobra.Command{
// It's a swarm cluster
util.PrintVerbose(fmt.Sprintf("Swarm cluster found with id %s", swarmClusterId))
deploymentErr := portainerClient.CreateSwarmStack(stackName, loadedEnvironmentVariables, stackFileContent, swarmClusterId, viper.GetString("stack.deploy.endpoint"))
util.CheckError(deploymentErr)
common.CheckError(deploymentErr)
case *valueNotFoundError:
// It's not a swarm cluster
util.PrintVerbose("Swarm cluster not found")
deploymentErr := portainerClient.CreateComposeStack(stackName, loadedEnvironmentVariables, stackFileContent, viper.GetString("stack.deploy.endpoint"))
util.CheckError(deploymentErr)
common.CheckError(deploymentErr)
default:
// Something else happened
util.CheckError(stackRetrievalErr)
common.CheckError(stackRetrievalErr)
}
default:
// Something else happened
util.CheckError(stackRetrievalErr)
common.CheckError(stackRetrievalErr)
}
},
}

View File

@ -20,24 +20,24 @@ var stackListCmd = &cobra.Command{
Example: "psu stack list --endpoint 1",
Run: func(cmd *cobra.Command, args []string) {
client, err := common.GetClient()
util.CheckError(err)
common.CheckError(err)
stacks, err := client.GetStacks(viper.GetString("stack.list.swarm"), viper.GetUint32("stack.list.endpoint"))
util.CheckError(err)
common.CheckError(err)
if viper.GetBool("stack.list.quiet") {
// Print only stack names
for _, s := range stacks {
_, err := fmt.Println(s.Name)
util.CheckError(err)
common.CheckError(err)
}
} else if viper.GetString("stack.list.format") != "" {
// Print stack fields formatted
template, templateParsingErr := template.New("stackTpl").Parse(viper.GetString("stack.list.format"))
util.CheckError(templateParsingErr)
common.CheckError(templateParsingErr)
for _, s := range stacks {
templateExecutionErr := template.Execute(os.Stdout, s)
util.CheckError(templateExecutionErr)
common.CheckError(templateExecutionErr)
fmt.Println()
}
} else {
@ -51,7 +51,7 @@ var stackListCmd = &cobra.Command{
"ENDPOINT ID",
"SWARM ID",
})
util.CheckError(err)
common.CheckError(err)
for _, s := range stacks {
_, err := fmt.Fprintln(writer, fmt.Sprintf(
"%v\t%s\t%v\t%s\t%s\t%v\t%s",
@ -63,10 +63,10 @@ var stackListCmd = &cobra.Command{
s.EndpointID,
s.SwarmID,
))
util.CheckError(err)
common.CheckError(err)
}
flushErr := writer.Flush()
util.CheckError(flushErr)
common.CheckError(flushErr)
}
},
}

View File

@ -32,10 +32,10 @@ var stackRemoveCmd = &cobra.Command{
util.PrintVerbose(fmt.Sprintf("Removing stack %s...", stackName))
client, err := common.GetClient()
util.CheckError(err)
common.CheckError(err)
err = client.DeleteStack(stackId)
util.CheckError(err)
common.CheckError(err)
case *common.StackNotFoundError:
// The stack does not exist
util.PrintVerbose(fmt.Sprintf("Stack %s does not exist.", stackName))
@ -44,7 +44,7 @@ var stackRemoveCmd = &cobra.Command{
}
default:
// Something else happened
util.CheckError(err)
common.CheckError(err)
}
},
}

View File

@ -18,17 +18,17 @@ var statusCmd = &cobra.Command{
Short: "Check Portainer status",
Run: func(cmd *cobra.Command, args []string) {
client, err := common.GetClient()
util.CheckError(err)
common.CheckError(err)
respBody, err := client.GetStatus()
util.CheckError(err)
common.CheckError(err)
if viper.GetString("status.format") != "" {
// Print stack fields formatted
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
util.CheckError(templateParsingErr)
common.CheckError(templateParsingErr)
templateExecutionErr := template.Execute(os.Stdout, respBody)
util.CheckError(templateExecutionErr)
common.CheckError(templateExecutionErr)
fmt.Println()
} else {
// Print status fields as a table
@ -38,7 +38,7 @@ var statusCmd = &cobra.Command{
"ENDPOINT MANAGEMENT",
"ANALYTICS",
})
util.CheckError(newTabWriterErr)
common.CheckError(newTabWriterErr)
_, printingErr := fmt.Fprintln(writer, fmt.Sprintf(
"%s\t%v\t%v\t%v",
@ -47,10 +47,10 @@ var statusCmd = &cobra.Command{
respBody.EndpointManagement,
respBody.Analytics,
))
util.CheckError(printingErr)
common.CheckError(printingErr)
flushErr := writer.Flush()
util.CheckError(flushErr)
common.CheckError(flushErr)
}
},
}

13
common/errors.go Normal file
View File

@ -0,0 +1,13 @@
package common
import (
"fmt"
"log"
)
// CheckError checks if an error occurred (it's not nil)
func CheckError(err error) {
if err != nil {
log.Fatalln(fmt.Sprintf("Error: %s", err.Error()))
}
}