Skip to content

Commit

Permalink
Makefile improvements, better error handling
Browse files Browse the repository at this point in the history
(#2), ToTitle()->Title(), spaces
between comment delimiters and start of comment.
  • Loading branch information
sarahcpage committed Oct 24, 2020
1 parent efa624f commit 78a2a9c
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 122 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
FROM golang:1.13 AS builder

WORKDIR /gobuild
Expand Down
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
EXECUTABLE=viya4-orders-cli
WINDOWS=$(EXECUTABLE)_windows_amd64.exe
LINUX=$(EXECUTABLE)_linux_amd64
DARWIN=$(EXECUTABLE)_darwin_amd64
MAIN=./main.go
VERSION=$(shell git describe --tags --abbrev=0 --always)
BUILDARGS=-s -w -X github.com/sassoftware/viya4-orders-cli/cmd.version=$(VERSION)
MAIN=./main.go
export GOFLAGS=-v -trimpath

win: $(WINDOWS) ## Build for Windows

Expand All @@ -14,13 +17,14 @@ darwin: $(DARWIN) ## Build for Darwin (Mac OS)

$(WINDOWS):
@echo version: $(VERSION)
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="$(BUILDARGS)" $(MAIN)
env GOOS=windows GOARCH=amd64 go build -o $(WINDOWS) -ldflags="$(BUILDARGS)" $(MAIN)

$(LINUX):
@echo version: $(VERSION)
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="$(BUILDARGS)" $(MAIN)
env GOOS=linux GOARCH=amd64 go build -o $(LINUX) -ldflags="$(BUILDARGS)" $(MAIN)
$(DARWIN):
@echo version: $(VERSION)
env GOOS=darwin GOARCH=amd64 go build -v -o $(DARWIN) -ldflags="$(BUILDARGS)" $(MAIN)
env GOOS=darwin GOARCH=amd64 go build -o $(DARWIN) -ldflags="$(BUILDARGS)" $(MAIN)
clean:
rm -f $(WINDOWS) $(LINUX) $(DARWIN)
build: win linux darwin
11 changes: 7 additions & 4 deletions cmd/certificates.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
//SPDX-License-Identifier: Apache-2.0
// Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/sassoftware/viya4-orders-cli/lib/assetreqs"

"github.com/spf13/cobra"
"log"
)

// certsCmd represents the certs command
Expand All @@ -20,7 +20,10 @@ var certificatesCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
ar := assetreqs.New(token, "certificates", args[0], "","",
assetFilePath, assetFileName, outFormat)
ar.GetAsset()
err := ar.GetAsset()
if err != nil {
log.Fatalln(err)
}
},
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/deploymentassets.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
//SPDX-License-Identifier: Apache-2.0
// Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/sassoftware/viya4-orders-cli/lib/assetreqs"
"github.com/spf13/cobra"
"log"
)

// depAssetsCmd represents the depassets command
Expand All @@ -19,12 +20,15 @@ var deploymentAssetsCmd = &cobra.Command{
Aliases: []string{"depassets", "dep"},
Args: cobra.RangeArgs(2,3),
Run: func(cmd *cobra.Command, args []string) {
var cver string = ""
var cver = ""
if len(args) == 3 {
cver = args[2]
}
ar := assetreqs.New(token, "deploymentAssets", args[0], args[1], cver, assetFilePath, assetFileName, outFormat)
ar.GetAsset()
err := ar.GetAsset()
if err != nil {
log.Fatalln(err)
}
},
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/license.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
//SPDX-License-Identifier: Apache-2.0
// Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/sassoftware/viya4-orders-cli/lib/assetreqs"
"github.com/spf13/cobra"
"log"
)

// licenseCmd represents the license command
Expand All @@ -18,7 +19,10 @@ var licenseCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
ar := assetreqs.New(token, "license", args[0], args[1], args[2], assetFilePath, assetFileName, outFormat)
ar.GetAsset()
err := ar.GetAsset()
if err != nil {
log.Fatalln(err)
}
},
}

Expand Down
27 changes: 15 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"fmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/sassoftware/viya4-orders-cli/lib/authn"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,7 +32,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 {
log.Panic(err.Error())
os.Exit(1)
}
}

Expand Down Expand Up @@ -63,7 +62,7 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Panic(err.Error())
log.Fatalln("ERROR: homedir.Dir() returned: " + err.Error())
}

// Search config in home directory with name ".viya4-orders-cli" (without extension).
Expand All @@ -77,24 +76,25 @@ func initConfig() {
err := viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Panic(err.Error())
log.Fatalln("ERROR: problem parsing config file " + viper.ConfigFileUsed() + ": " + err.Error())
}
}

if outFormat != "j" && outFormat != "json" {
if viper.ConfigFileUsed() != "" {
log.Println("Using config file:", viper.ConfigFileUsed())
log.Println("INFO: using config file:", viper.ConfigFileUsed())
} else {
log.Println("No config file found")
log.Println("INFO: no config file found")
}
}

// Read in environment vars.
viper.AutomaticEnv()

// Bind flags from the command line to the Viper framework.
if err := viper.BindPFlags(rootCmd.Flags()); err != nil {
log.Panic(err)
err = viper.BindPFlags(rootCmd.Flags())
if err != nil {
log.Fatalln("ERROR: viper.BindPFlags() returned: " + err.Error())
}

setOptions()
Expand All @@ -121,17 +121,20 @@ func setOptions() {
outFormat := viper.GetString("output")
// Validate output flag value.
if outFormat != "text" && outFormat != "t" && outFormat != "json" && outFormat != "j" {
usageError("Invalid value " + outFormat + " specified for -o, --output option!")
usageError("invalid value " + outFormat + " specified for -o, --output option!")
}
}

func usageError(message string) {
log.Fatalln("Error: " + message)
rootCmd.Help()
fmt.Println("Error: " + message)
os.Exit(1)
}

// Get Bearer token.
func auth() {
token = authn.GetBearerToken()
var err error
token, err = authn.GetBearerToken()
if err != nil {
log.Fatalln(err.Error())
}
}
Loading

0 comments on commit 78a2a9c

Please sign in to comment.