Skip to content

Commit acf07d2

Browse files
author
Oleg Sucharevich
committed
Extend readme with basic example
Remove generated comments from commands
1 parent 20b733c commit acf07d2

File tree

8 files changed

+50
-115
lines changed

8 files changed

+50
-115
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
# Codefresh SDK for Golang
1+
# Codefresh SDK for Golang
2+
3+
# Start
4+
5+
`go get -u github.com/codefresh-io`
6+
7+
```go
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/codefresh-io/go-sdk/pkg/util"
14+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
15+
)
16+
17+
func main() {
18+
path := fmt.Sprintf("%s/.cfconfig", os.Getenv("HOME"))
19+
authOptions := util.ReadAuthContext(path, "")
20+
cf := codefresh.New(authOptions)
21+
cf.GetPipelines()
22+
}
23+
```

cmd/create.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,36 +15,13 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/spf13/cobra"
2119
)
2220

23-
// createCmd represents the create command
2421
var createCmd = &cobra.Command{
25-
Use: "create",
26-
Short: "A brief description of your command",
27-
Long: `A longer description that spans multiple lines and likely contains examples
28-
and usage of using your command. For example:
29-
30-
Cobra is a CLI library for Go that empowers applications.
31-
This application is a tool to generate the needed files
32-
to quickly create a Cobra application.`,
33-
Run: func(cmd *cobra.Command, args []string) {
34-
fmt.Println("create called")
35-
},
22+
Use: "create",
3623
}
3724

3825
func init() {
3926
rootCmd.AddCommand(createCmd)
40-
41-
// Here you will define your flags and configuration settings.
42-
43-
// Cobra supports Persistent Flags which will work for this command
44-
// and all subcommands, e.g.:
45-
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
46-
47-
// Cobra supports local flags which will only run when this command
48-
// is called directly, e.g.:
49-
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
5027
}

cmd/create_token.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,25 +16,18 @@ package cmd
1616

1717
import (
1818
"github.com/codefresh-io/go-sdk/internal"
19-
"github.com/codefresh-io/go-sdk/pkg/codefresh"
19+
"github.com/codefresh-io/go-sdk/pkg/utils"
2020
"github.com/spf13/cobra"
2121
"github.com/spf13/viper"
2222
)
2323

24-
// createTokenCmd represents the createToken command
2524
var createTokenCmd = &cobra.Command{
2625
Use: "tokens",
2726
Aliases: []string{"token"},
2827
Short: "Create tokens",
29-
Long: `A longer description that spans multiple lines and likely contains examples
30-
and usage of using your command. For example:
31-
32-
Cobra is a CLI library for Go that empowers applications.
33-
This application is a tool to generate the needed files
34-
to quickly create a Cobra application.`,
3528
Run: func(cmd *cobra.Command, args []string) {
3629
client := viper.Get("codefresh")
37-
codefreshClient := client.(codefresh.Codefresh)
30+
codefreshClient := utils.CastToCodefreshOrDie(client)
3831
token := codefreshClient.GenerateToken("TestToken", "hybrid/codefresh-re")
3932
table := internal.CreateTable()
4033
table.SetHeader([]string{"name", "token"})
@@ -45,14 +38,4 @@ to quickly create a Cobra application.`,
4538

4639
func init() {
4740
createCmd.AddCommand(createTokenCmd)
48-
49-
// Here you will define your flags and configuration settings.
50-
51-
// Cobra supports Persistent Flags which will work for this command
52-
// and all subcommands, e.g.:
53-
// createTokenCmd.PersistentFlags().String("foo", "", "A help for foo")
54-
55-
// Cobra supports local flags which will only run when this command
56-
// is called directly, e.g.:
57-
// createTokenCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
5841
}

cmd/get.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,35 +15,13 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/spf13/cobra"
2119
)
2220

23-
// getCmd represents the get command
2421
var getCmd = &cobra.Command{
25-
Use: "get",
26-
Short: "A brief description of your command",
27-
Long: `A longer description that spans multiple lines and likely contains examples
28-
and usage of using your command. For example:
29-
30-
Cobra is a CLI library for Go that empowers applications.
31-
This application is a tool to generate the needed files
32-
to quickly create a Cobra application.`,
33-
Run: func(cmd *cobra.Command, args []string) {
34-
fmt.Println("get called")
35-
},
22+
Use: "get",
3623
}
3724

3825
func init() {
3926
rootCmd.AddCommand(getCmd)
40-
// Here you will define your flags and configuration settings.
41-
42-
// Cobra supports Persistent Flags which will work for this command
43-
// and all subcommands, e.g.:
44-
// getCmd.PersistentFlags().String("foo", "", "A help for foo")
45-
46-
// Cobra supports local flags which will only run when this command
47-
// is called directly, e.g.:
48-
// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4927
}

cmd/get_pipeline.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -28,12 +28,6 @@ import (
2828
var getPipelineCmd = &cobra.Command{
2929
Use: "pipeline",
3030
Short: "Get pipelines",
31-
Long: `A longer description that spans multiple lines and likely contains examples
32-
and usage of using your command. For example:
33-
34-
Cobra is a CLI library for Go that empowers applications.
35-
This application is a tool to generate the needed files
36-
to quickly create a Cobra application.`,
3731
Run: func(cmd *cobra.Command, args []string) {
3832
client := viper.Get("codefresh")
3933
codefreshClient, ok := client.(codefresh.Codefresh)

cmd/get_tokens.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,29 +18,19 @@ import (
1818
"fmt"
1919

2020
"github.com/codefresh-io/go-sdk/internal"
21-
"github.com/codefresh-io/go-sdk/pkg/codefresh"
21+
"github.com/codefresh-io/go-sdk/pkg/utils"
2222
humanize "github.com/dustin/go-humanize"
2323
"github.com/spf13/cobra"
2424
"github.com/spf13/viper"
2525
)
2626

27-
// getTokensCmd represents the getTokens command
2827
var getTokensCmd = &cobra.Command{
2928
Use: "tokens",
3029
Aliases: []string{"token"},
31-
Short: "A brief description of your command",
32-
Long: `A longer description that spans multiple lines and likely contains examples
33-
and usage of using your command. For example:
34-
35-
Cobra is a CLI library for Go that empowers applications.
36-
This application is a tool to generate the needed files
37-
to quickly create a Cobra application.`,
30+
Short: "Get tokens",
3831
Run: func(cmd *cobra.Command, args []string) {
3932
client := viper.Get("codefresh")
40-
codefreshClient, ok := client.(codefresh.Codefresh)
41-
if !ok {
42-
panic("Faild to create Codefresh cleint")
43-
}
33+
codefreshClient := utils.CastToCodefreshOrDie(client)
4434
table := internal.CreateTable()
4535
table.SetHeader([]string{"Created", "ID", "Name", "Reference Subject", "Reference Type", "Token"})
4636
table.Append([]string{"", "", ""})
@@ -61,14 +51,4 @@ to quickly create a Cobra application.`,
6151

6252
func init() {
6353
getCmd.AddCommand(getTokensCmd)
64-
65-
// Here you will define your flags and configuration settings.
66-
67-
// Cobra supports Persistent Flags which will work for this command
68-
// and all subcommands, e.g.:
69-
// getTokensCmd.PersistentFlags().String("foo", "", "A help for foo")
70-
71-
// Cobra supports local flags which will only run when this command
72-
// is called directly, e.g.:
73-
// getTokensCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
7454
}

cmd/root.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
1+
// Copyright © 2018 Codefresh.Inc
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,20 +27,10 @@ import (
2727

2828
var cfgFile string
2929

30-
// rootCmd represents the base command when called without any subcommands
3130
var rootCmd = &cobra.Command{
3231
Use: "cfctl",
3332
Short: "A command line application for Codefresh",
34-
Long: `A longer description that spans multiple lines and likely contains
35-
examples and usage of using your application. For example:
36-
37-
Cobra is a CLI library for Go that empowers applications.
38-
This application is a tool to generate the needed files
39-
to quickly create a Cobra application.`,
40-
// Uncomment the following line if your bare application
41-
// has an action associated with it:
42-
// Run: func(cmd *cobra.Command, args []string) {
43-
// },
33+
Long: ``,
4434

4535
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
4636
configPath := cmd.Flag("cfconfig").Value.String()
@@ -65,8 +55,6 @@ to quickly create a Cobra application.`,
6555
},
6656
}
6757

68-
// Execute adds all child commands to the root command and sets flags appropriately.
69-
// This is called by main.main(). It only needs to happen once to the rootCmd.
7058
func Execute() {
7159
if err := rootCmd.Execute(); err != nil {
7260
fmt.Println(err)

pkg/utils/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package utils
2+
3+
import (
4+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
5+
)
6+
7+
func CastToCodefreshOrDie(candidate interface{}) codefresh.Codefresh {
8+
client, ok := candidate.(codefresh.Codefresh)
9+
if !ok {
10+
panic("Failed to cast candidate to Codefresh client")
11+
}
12+
return client
13+
}

0 commit comments

Comments
 (0)