Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WolverMinion committed Nov 11, 2020
1 parent 1213e79 commit c7bb27a
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 35 deletions.
5 changes: 4 additions & 1 deletion cmd/check/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package check

import (
"cx/pkg/output"
"fmt"
"github.com/spf13/cobra"
)
Expand All @@ -11,7 +12,9 @@ func NewCmdCheck() *cobra.Command {
Short: "check with sub commands",
Long: `choose a check`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("use %s", cmd.Name())
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/check/ghprotection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func NewCmdCGhPtotection() *cobra.Command {
Short: "check github protection",
Long: `compliance check for github repository protection`,
Run: func(cmd *cobra.Command, args []string) {
output.PrintCliInfo(fmt.Sprintf("use %s", cmd.Name()))
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

compliantState := github.GHRepository{
Organisation: "",
Expand Down
5 changes: 4 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"cx/pkg/output"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -32,7 +33,9 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("install called")
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

},
}

Expand Down
26 changes: 15 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ package cmd

import (
"cx/cmd/check"
"cx/pkg/output"
"fmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/component-base/logs"
"os"
"runtime"
"k8s.io/component-base/logs"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string
Expand All @@ -32,7 +33,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "cx",
Short: "cx componenten installer",
Long: `....`,
Long: `....`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -44,7 +45,7 @@ var rootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
output.PrintCliError(err)
os.Exit(1)
}
}
Expand Down Expand Up @@ -77,7 +78,7 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
output.PrintCliError(err)
os.Exit(1)
}

Expand All @@ -92,13 +93,16 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
output.PrintCliInfo(fmt.Sprintf("Using config file:", viper.ConfigFileUsed()))
}
}

func printInfo() {
fmt.Printf("Operating System: %s\nArchitecture: %s\n", runtime.GOOS, runtime.GOARCH)
fmt.Println("Check our Sources at https://github.com/Continuous-X/cx")
fmt.Println("Get in contact via github issue.....")
fmt.Printf("Author: %s", viper.GetString("author"))
output.PrintCliInfo(fmt.Sprintf("%s\n%s\n%s\n%s",
fmt.Sprintf("Operating System: %s\nArchitecture: %s", runtime.GOOS, runtime.GOARCH),
fmt.Sprint("Check our Sources at https://github.com/Continuous-X/cx"),
fmt.Sprintf("Get in contact via github issue....."),
fmt.Sprintf("Author: %s", viper.GetString("author")),
))

}
5 changes: 4 additions & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"cx/pkg/output"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -32,7 +33,9 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("uninstall called")
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"cx/pkg/output"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -32,7 +33,9 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("update called")
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"cx/pkg/output"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -32,7 +33,9 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("upgrade called")
message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)

},
}

Expand Down
18 changes: 15 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ limitations under the License.
package cmd

import (
error2 "cx/pkg/error"
"cx/pkg/versions"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"runtime"
"k8s.io/apimachinery/pkg/version"
"runtime"
)

// versionCmd represents the versions command
Expand All @@ -34,7 +36,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
printVersion()
/* message := fmt.Sprintf("use %s", cmd.Name())
output.PrintCliInfo(message)
*/
versionInfo := get()
marshalled, err := json.MarshalIndent(&versionInfo, "", " ")
if err != nil {
error2.FailHandleCommand(err)
}
fmt.Println(string(marshalled))

//printVersion()
},
}

Expand All @@ -59,7 +71,7 @@ func printVersion() {
fmt.Printf("SHA: %s\n", versions.CommitFromGit)
}

func Get() version.Info {
func get() version.Info {
return version.Info{
Major: versions.MajorFromGit,
Minor: versions.MinorFromGit,
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ limitations under the License.
package main

import (
"cx/pkg/versions"
"cx/cmd"
"cx/pkg/versions"
"strings"
)

var (
sha1ver string
buildTime string
version string
version string
)

func main() {
versions.CommitFromGit = sha1ver
versions.VersionFromGit = version
versionArray := strings.Split(version, ".")
if len(version) > 0 {
versions.MajorFromGit = versionArray[0]
}
if len(version) > 1 {
versions.MinorFromGit = versionArray[1]
}
versions.BuildDate = buildTime
cmd.Execute()
}
9 changes: 9 additions & 0 deletions pkg/error/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package error

import "cx/pkg/output"

func FailHandleCommand(err error) {
if err != nil {
output.PrintCliError(err)
}
}
24 changes: 11 additions & 13 deletions pkg/output/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,34 @@ const (
MessageStateError = "ERROR"
)



func PrintCliInfo(msg string) {
PrintYellow(fmt.Sprintf("%s [INFO]: ",MessagePrefixCli))
func PrintCliInfo(msg string) {
PrintYellow(fmt.Sprintf("%s [INFO]: ", MessagePrefixCli))
PrintWhite(msg)
}

func PrintCliWarning(msg string) {
PrintYellow(fmt.Sprintf("%s [WARNING]: ",MessagePrefixCli))
func PrintCliWarning(msg string) {
PrintYellow(fmt.Sprintf("%s [WARNING]: ", MessagePrefixCli))
PrintWhite(msg)
}

func PrintCliError(msg string) {
PrintRed(fmt.Sprintf("%s [ERROR]: ",MessagePrefixCli))
PrintWhite(msg)
func PrintCliError(err error) {
PrintRed(fmt.Sprintf("%s [ERROR]: ", MessagePrefixCli))
PrintWhite(fmt.Sprintf("%s", err))
}

func PrintCheckGhProtectionSuccess() {
func PrintCheckGhProtectionSuccess() {
PrintCommandSuccess(MessagePrefixCommandCheckGhProtection)
}

func PrintCheckGhProtectionError() {
func PrintCheckGhProtectionError() {
PrintCommandError(MessagePrefixCommandCheckGhProtection)
}

func PrintCommandSuccess(command string) {
func PrintCommandSuccess(command string) {
PrintGreen(fmt.Sprintf("%s: %s", command, MessageStateSuccess))
}

func PrintCommandError(command string) {
func PrintCommandError(command string) {
PrintRed(fmt.Sprintf("%s: %s", command, MessageStateError))
}

Expand Down

0 comments on commit c7bb27a

Please sign in to comment.