Skip to content

Commit 136925c

Browse files
authored
made pipelines runtime non interactive (#31)
1 parent 808993e commit 136925c

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

cmd/pipelines.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,69 @@ import (
3232
"github.com/spf13/cobra"
3333
)
3434

35+
var pipelinesNamespace string
36+
var pipelinesRuntime string
37+
3538
// pipelinesCmd represents the pipelines command
3639
var pipelinesCmd = &cobra.Command{
3740
Use: "pipelines",
3841
Short: "Collect data for the Codefresh Pipelines Runtime",
3942
Long: `Collect data for the Codefresh Pipelines Runtime`,
4043
Run: func(cmd *cobra.Command, args []string) {
4144
const RuntimeType = "Codefresh Pipelines Runtime"
45+
var runtimes []map[string]interface{}
46+
var reSpec map[string]interface{}
47+
4248
dirPath := fmt.Sprintf("./codefresh-support-%d", time.Now().Unix())
49+
4350
cfConfig, err := codefresh.GetCodefreshCreds()
4451
if err != nil {
4552
cmd.PrintErrln("Error getting Codefresh credentials:", err)
46-
return
4753
}
4854

49-
runtimes, err := codefresh.AccountRuntimes(cfConfig)
50-
if err != nil {
51-
cmd.PrintErrln("Error getting Codefresh runtimes:", err)
52-
return
53-
}
55+
if cfConfig != nil {
56+
if pipelinesRuntime != "" {
57+
reSpec, err = codefresh.SingleRuntime(cfConfig, pipelinesRuntime)
58+
if err != nil {
59+
cmd.PrintErrln("Error getting Codefresh runtimes:", err)
60+
}
5461

55-
var pipelinesNamespace string
56-
var reSpec map[string]interface{}
62+
} else {
63+
runtimes, err = codefresh.AccountRuntimes(cfConfig)
64+
if err != nil {
65+
cmd.PrintErrln("Error getting Codefresh runtimes:", err)
66+
}
5767

58-
if len(runtimes) != 0 {
59-
var selection int
60-
for index, runtime := range runtimes {
61-
cmd.Printf("%d. %s\n", index+1, runtime["metadata"].(map[string]interface{})["name"])
62-
}
63-
for {
64-
cmd.Print("\nPlease select the runtime to gather data from (Number): ")
65-
_, err := fmt.Scanf("%d", &selection)
66-
if err != nil || selection < 1 || selection > len(runtimes) {
67-
cmd.PrintErrln("Invalid selection. Please enter a number corresponding to one of the listed runtimes.")
68-
continue
68+
if len(runtimes) != 0 {
69+
var selection int
70+
for index, runtime := range runtimes {
71+
cmd.Printf("%d. %s\n", index+1, runtime["metadata"].(map[string]interface{})["name"])
72+
}
73+
for {
74+
cmd.Print("\nPlease select the runtime to gather data from (Number): ")
75+
_, err := fmt.Scanf("%d", &selection)
76+
if err != nil || selection < 1 || selection > len(runtimes) {
77+
cmd.PrintErrln("Invalid selection. Please enter a number corresponding to one of the listed runtimes.")
78+
continue
79+
}
80+
break
81+
82+
}
83+
reSpec = runtimes[selection-1]
6984
}
70-
break
7185

7286
}
73-
reSpec = runtimes[selection-1]
74-
pipelinesNamespace = reSpec["runtimeScheduler"].(map[string]interface{})["cluster"].(map[string]interface{})["namespace"].(string)
75-
} else {
76-
cmd.Println("No runtimes found in Codefresh account.")
77-
pipelinesNamespace, err = k8s.SelectNamespace(RuntimeType)
78-
if err != nil {
79-
cmd.PrintErrf("error getting Kubernetes namespace: %v", err)
80-
return
87+
}
88+
89+
if pipelinesNamespace == "" {
90+
if reSpec != nil {
91+
pipelinesNamespace = reSpec["runtimeScheduler"].(map[string]interface{})["cluster"].(map[string]interface{})["namespace"].(string)
92+
} else {
93+
pipelinesNamespace, err = k8s.SelectNamespace(RuntimeType)
94+
if err != nil {
95+
cmd.PrintErrf("error getting Kubernetes namespace: %v", err)
96+
return
97+
}
8198
}
8299
}
83100

@@ -119,4 +136,6 @@ func init() {
119136
// Cobra supports local flags which will only run when this command
120137
// is called directly, e.g.:
121138
// pipelinesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
139+
pipelinesCmd.Flags().StringVarP(&pipelinesNamespace, "namespace", "n", "", "The namespace where the Runtime is installed")
140+
pipelinesCmd.Flags().StringVarP(&pipelinesRuntime, "runtime", "re", "", "The name of the Pipelines Runtime")
122141
}

internal/codefresh/account_runtimes.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"net/url"
78
)
89

910
func AccountRuntimes(cfConfig *CodefreshConfig) ([]map[string]interface{}, error) {
@@ -33,3 +34,32 @@ func AccountRuntimes(cfConfig *CodefreshConfig) ([]map[string]interface{}, error
3334

3435
return runtimes, nil
3536
}
37+
38+
func SingleRuntime(cfConfig *CodefreshConfig, runtimeName string) (map[string]interface{}, error) {
39+
40+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/runtime-environments/%s", cfConfig.BaseURL, url.PathEscape(runtimeName)), nil)
41+
if err != nil {
42+
return nil, err
43+
}
44+
for key, value := range cfConfig.Headers {
45+
req.Header.Set(key, value)
46+
}
47+
48+
client := &http.Client{}
49+
resp, err := client.Do(req)
50+
if err != nil {
51+
return nil, err
52+
}
53+
defer resp.Body.Close()
54+
55+
if resp.StatusCode != http.StatusOK {
56+
return nil, fmt.Errorf("failed to get runtime environment: %s", resp.Status)
57+
}
58+
59+
var runtime map[string]interface{}
60+
if err := json.NewDecoder(resp.Body).Decode(&runtime); err != nil {
61+
return nil, err
62+
}
63+
64+
return runtime, nil
65+
}

0 commit comments

Comments
 (0)