Skip to content

Commit

Permalink
Fix golangci lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Sep 24, 2023
1 parent 8de0385 commit 11defd5
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 7 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- gci
- wsl
- gochecknoglobals
- tagliatelle
- exhaustivestruct
Expand Down
1 change: 1 addition & 0 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func getAuthEraseCommand() *cobra.Command {

func checkForConfig() (bool, string, error) {
cliLogger.Debug("searching for authorisation configuration in cache")

home, err := os.UserHomeDir()
if err != nil {
return false, "", err
Expand Down
7 changes: 7 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ func setCLIClient(_ *cobra.Command, _ []string) error {

if localConfig && !cliCfg.skipCacheConfig {
cliLogger.Debugf("found authorisation configuration in cache, loading config from %s", localConfigPath)

yamlConfig, err := os.ReadFile(localConfigPath)
if err != nil {
return err
}

if err = yaml.Unmarshal(yamlConfig, &cliCfg); err != nil {
return err
}

cliLogger.Debug("authorisation configuration loaded from cache successfully")
}

if len(cliCfg.CaPath) != 0 {
cliLogger.Debug("CA based auth is enabled, hence reading ca from the path")

caAbs, err := filepath.Abs(cliCfg.CaPath)
if err != nil {
return err
Expand All @@ -61,12 +65,15 @@ func setCLIClient(_ *cobra.Command, _ []string) error {
client = goCDClient

writer := os.Stdout

if len(cliCfg.ToFile) != 0 {
cliLogger.Debugf("--to-file is opted, output would be saved under a file '%s'", cliCfg.ToFile)

filePTR, err := os.Create(cliCfg.ToFile)
if err != nil {
return err
}

writer = filePTR
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var cmd *cobra.Command
//nolint:gochecknoinits
func init() {
cmd = SetGoCDCliCommands()

log.SetFlags(log.LstdFlags | log.Lshortfile)
}

Expand All @@ -25,6 +26,7 @@ func Main() {
// execute will actually execute the cli by taking the arguments passed to cli.
func execute(args []string) error {
cmd.SetArgs(args)

if _, err := cmd.ExecuteC(); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ func lastUpdatedCommit(date string) float64 {
if err != nil {
log.Fatalln(err)
}

parsedTime := tm.In(loc)

timeNow := time.Now().In(loc)
Expand Down
8 changes: 8 additions & 0 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ func getPipelineMapping() *cobra.Command {

func findDownStreamPipelines(pipelineName string, resp gocd.VSM) []string {
newParents := []string{pipelineName}

for _, level := range resp.Level {
for _, node := range level.Nodes {
for _, newParent := range newParents {
Expand All @@ -1051,6 +1052,7 @@ func findDownStreamPipelines(pipelineName string, resp gocd.VSM) []string {

func findUpStreamPipelines(pipelineName string, resp gocd.VSM) []string {
newChilds := []string{pipelineName}

for _, level := range resp.Level {
for _, node := range level.Nodes {
for _, newChild := range newChilds {
Expand Down Expand Up @@ -1106,26 +1108,32 @@ func parsePipelineConfig(pipelineName string, pipelineStreams []string) ([]strin
}

pipelineCfg := string(bytes)

if gjson.Valid(pipelineCfg) {
for _, material := range gjson.Get(pipelineCfg, "materials").Array() {
if funk.Contains(gjson.Get(material.String(), "attributes.name").String(), pipelineName) {
pipelineDependencies = append(pipelineDependencies, pipelineStream)
}

if funk.Contains(gjson.Get(material.String(), "attributes.url").String(), pipelineName) {
pipelineDependencies = append(pipelineDependencies, pipelineStream)
}

if funk.Contains(gjson.Get(material.String(), "attributes.pipeline").String(), pipelineName) {
pipelineDependencies = append(pipelineDependencies, pipelineStream)
}
}

for _, material := range gjson.Get(pipelineCfg, "parameters").Array() {
if funk.Contains(gjson.Get(material.String(), "name").String(), pipelineName) {
pipelineDependencies = append(pipelineDependencies, pipelineStream)
}

if funk.Contains(gjson.Get(material.String(), "value").String(), pipelineName) {
pipelineDependencies = append(pipelineDependencies, pipelineStream)
}
}

for _, stage := range gjson.Get(pipelineCfg, "stages").Array() {
for _, job := range gjson.Get(stage.String(), "jobs").Array() {
for _, tasks := range gjson.Get(job.String(), "tasks").Array() {
Expand Down
6 changes: 6 additions & 0 deletions cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ import (

func readObject(cmd *cobra.Command) (render.Object, error) {
var obj render.Object

if len(cliCfg.FromFile) != 0 {
cliLogger.Debug("reading configuration object from file since --from-file is enabled")

data, err := os.ReadFile(cliCfg.FromFile)
if err != nil {
return obj, err
}

obj = render.Object(data)
} else {
cliLogger.Debug("reading configuration object from stdin")

stdIn := cmd.InOrStdin()

in, err := io.ReadAll(stdIn)
if err != nil {
return obj, err
}

obj = render.Object(in)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/server_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func artifactConfigGetCommand() *cobra.Command {

func artifactConfigUpdateCommand() *cobra.Command {
var artifactDir string

var purgeStartDiskSpace, purgeUptoDiskSpace float64

artifactConfigUpdateCmd := &cobra.Command{
Expand Down
12 changes: 8 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,29 @@ func AppVersion(_ *cobra.Command, _ []string) error {
writer := bufio.NewWriter(os.Stdout)

var serverVersionInfo string

cliLogger.Debug("a call to GoCD server would be made to collect server version")

if serverVersion, _ := client.GetVersionInfo(); !reflect.DeepEqual(serverVersion, gocd.ServerVersion{}) {
cliLogger.Debug("got an update from GoCD server about server version")

serverVersionJSON, err := json.Marshal(serverVersion)
if err != nil {
cliLogger.Errorf("fetching version of GoCD server failed with %v\n", err)
}

serverVersionInfo = strings.Join([]string{"server version", string(serverVersionJSON), "\n"}, ": ")

_, err = writer.Write([]byte(serverVersionInfo)) //nolint:mirror
if err != nil {
//nolint:mirror
if _, err = writer.Write([]byte(serverVersionInfo)); err != nil {
log.Fatalln(err)
}
}

cliVersionInfo := strings.Join([]string{"client version", string(buildInfo), "\n"}, ": ")
_, err = writer.Write([]byte(cliVersionInfo)) //nolint:mirror
if err != nil {

//nolint:mirror
if _, err = writer.Write([]byte(cliVersionInfo)); err != nil {
log.Fatalln(err)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/who_am_i.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func registerWhoAmICommand() *cobra.Command {
var raw bool

whoCmd := &cobra.Command{
Use: "who-am-i",
Short: "Command to check which user being used by GoCD Command line interface",
Expand Down
4 changes: 2 additions & 2 deletions docs/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
//go:generate go run github.com/nikhilsbhat/gocd-cli/docs
func main() {
commands := cmd.SetGoCDCliCommands()
err := doc.GenMarkdownTree(commands, "doc")
if err != nil {

if err := doc.GenMarkdownTree(commands, "doc"); err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions pkg/render/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func IsYAML(content string) bool {
// CheckFileType checks the file type of the content passed, it validates for YAML/JSON.
func (obj Object) CheckFileType(log *logrus.Logger) string {
log.Debug("identifying the input file type, only YAML/JSON is allowed")

if IsJSON(string(obj)) {
log.Debug("input file type identified as JSON")

Expand Down
1 change: 1 addition & 0 deletions pkg/render/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (q *Query) ConstructQuery(query string) {
q.object = queryLHSObject
queryRHS := strings.TrimLeftFunc(queryLHS[1], unicode.IsSpace)
queryRHSObject := strings.Split(queryRHS, " ")

switch len(queryRHSObject) {
case defaultLengthGetOrPluck:
q.key = queryRHSObject[0]
Expand Down
2 changes: 2 additions & 0 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (r *Renderer) Render(value interface{}) error {

func (r *Renderer) toYAML(value interface{}) error {
r.logger.Debug("rendering output in yaml format since --yaml is enabled")

valueYAML, err := yaml.Marshal(value)
if err != nil {
return err
Expand All @@ -75,6 +76,7 @@ func (r *Renderer) toYAML(value interface{}) error {

func (r *Renderer) toJSON(value interface{}) error {
r.logger.Debug("rendering output in json format since --json is enabled")

valueJSON, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
Expand Down

0 comments on commit 11defd5

Please sign in to comment.