Skip to content

Commit cbe4c31

Browse files
author
JulesFaucherre
authored
Merge pull request #911 from CircleCI-Public/develop
Release
2 parents 63309c6 + 5b836ba commit cbe4c31

25 files changed

+181
-80
lines changed

api/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,9 @@ func FollowProject(config settings.Config, vcs string, owner string, projectName
18341834
}
18351835
r.Header.Set("Content-Type", "application/json; charset=utf-8")
18361836
r.Header.Set("Accept", "application/json; charset=utf-8")
1837-
r.Header.Set("Circle-Token", config.Token)
1837+
if config.Token != "" {
1838+
r.Header.Set("Circle-Token", config.Token)
1839+
}
18381840

18391841
response, err := config.HTTPClient.Do(r)
18401842
if err != nil {

api/context_rest.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ func (c *ContextRestClient) newHTTPRequest(method, url string, body io.Reader) (
544544
if err != nil {
545545
return nil, err
546546
}
547-
req.Header.Add("circle-token", c.token)
547+
if c.token != "" {
548+
req.Header.Add("circle-token", c.token)
549+
}
548550
req.Header.Add("Accept", "application/json")
549551
req.Header.Add("Content-Type", "application/json")
550552
req.Header.Add("User-Agent", version.UserAgent())

api/info/info.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func (c *InfoRESTClient) newHTTPRequest(method, url string, body io.Reader) (*ht
8686
if err != nil {
8787
return nil, err
8888
}
89-
req.Header.Add("circle-token", c.token)
89+
if c.token != "" {
90+
req.Header.Add("circle-token", c.token)
91+
}
9092
req.Header.Add("Accept", "application/json")
9193
req.Header.Add("Content-Type", "application/json")
9294
req.Header.Add("User-Agent", version.UserAgent())

api/policy/policy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ func NewClient(baseURL string, config *settings.Config) *Client {
341341
// releasing the semaphore after a second ensuring client doesn't make more than cap(sem)/second
342342
time.AfterFunc(time.Second, func() { <-sem })
343343

344-
r.Header.Add("circle-token", config.Token)
344+
if config.Token != "" {
345+
r.Header.Add("circle-token", config.Token)
346+
}
345347
r.Header.Add("Accept", "application/json")
346348
r.Header.Add("Content-Type", "application/json")
347349
r.Header.Add("User-Agent", version.UserAgent())

api/rest/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func (c *Client) NewRequest(method string, u *url.URL, payload interface{}) (req
7070
}
7171

7272
func (c *Client) enrichRequestHeaders(req *http.Request, payload interface{}) {
73-
req.Header.Set("Circle-Token", c.circleToken)
73+
if c.circleToken != "" {
74+
req.Header.Set("Circle-Token", c.circleToken)
75+
}
7476
req.Header.Set("Accept", "application/json")
7577
req.Header.Set("User-Agent", version.UserAgent())
7678
commandStr := header.GetCommandStr()

api/schedule_rest.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,9 @@ func (c *ScheduleRestClient) newHTTPRequest(method, url string, body io.Reader)
402402
if err != nil {
403403
return nil, err
404404
}
405-
req.Header.Add("circle-token", c.token)
405+
if c.token != "" {
406+
req.Header.Add("circle-token", c.token)
407+
}
406408
req.Header.Add("Accept", "application/json")
407409
req.Header.Add("Content-Type", "application/json")
408410
req.Header.Add("User-Agent", version.UserAgent())

cmd/env.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/a8m/envsubst"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func newEnvCmd() *cobra.Command {
12+
var envCmd = &cobra.Command{
13+
Use: "env",
14+
Short: "Manage environment variables",
15+
}
16+
var substCmd = &cobra.Command{
17+
Use: "subst",
18+
Short: "Substitute environment variables in a string",
19+
RunE: substRunE,
20+
}
21+
envCmd.AddCommand(substCmd)
22+
return envCmd
23+
}
24+
25+
// Accepts a string as an argument, or reads from stdin if no argument is provided.
26+
func substRunE(cmd *cobra.Command, args []string) error {
27+
var input string
28+
if len(args) > 0 {
29+
input = args[0]
30+
} else {
31+
// Read from stdin
32+
b, err := io.ReadAll(cmd.InOrStdin())
33+
if err != nil {
34+
return err
35+
}
36+
input = string(b)
37+
}
38+
if input == "" {
39+
return nil
40+
}
41+
output, err := envsubst.String(input)
42+
if err != nil {
43+
return err
44+
}
45+
_, err = fmt.Fprint(cmd.OutOrStdout(), output)
46+
return err
47+
}

cmd/env_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
8+
"gotest.tools/v3/assert"
9+
)
10+
11+
func TestSubstRunE(t *testing.T) {
12+
// Set environment variables for testing
13+
err := os.Setenv("ENV_NAME", "world")
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
18+
testCases := []struct {
19+
name string
20+
input string
21+
output string
22+
}{
23+
{
24+
name: "substitute variables",
25+
input: "Hello $ENV_NAME!",
26+
output: "Hello world!",
27+
},
28+
{
29+
name: "no variables to substitute",
30+
input: "Hello, world!",
31+
output: "Hello, world!",
32+
},
33+
{
34+
name: "empty input",
35+
input: "",
36+
output: "",
37+
},
38+
{
39+
name: "no variables JSON",
40+
input: `{"foo": "bar"}`,
41+
output: `{"foo": "bar"}`,
42+
},
43+
{
44+
name: "substitute variables JSON",
45+
input: `{"foo": "$ENV_NAME"}`,
46+
output: `{"foo": "world"}`,
47+
},
48+
{
49+
name: "no variables key=value",
50+
input: `foo=bar`,
51+
output: `foo=bar`,
52+
},
53+
}
54+
55+
// Run tests for each test case as argument
56+
for _, tc := range testCases {
57+
t.Run("arg: "+tc.name, func(t *testing.T) {
58+
// Set up test command
59+
cmd := newEnvCmd()
60+
61+
// Capture output
62+
outputBuf := bytes.Buffer{}
63+
cmd.SetOut(&outputBuf)
64+
65+
// Run command
66+
cmd.SetArgs([]string{"subst", tc.input})
67+
err := cmd.Execute()
68+
69+
// Check output and error
70+
assert.NilError(t, err)
71+
assert.Equal(t, tc.output, outputBuf.String())
72+
})
73+
}
74+
// Run tests for each test case as stdin
75+
for _, tc := range testCases {
76+
t.Run("stdin: "+tc.name, func(t *testing.T) {
77+
// Set up test command
78+
cmd := newEnvCmd()
79+
80+
// Set up input
81+
inputBuf := bytes.NewBufferString(tc.input)
82+
cmd.SetIn(inputBuf)
83+
84+
// Capture output
85+
outputBuf := bytes.Buffer{}
86+
cmd.SetOut(&outputBuf)
87+
88+
// Run command
89+
cmd.SetArgs([]string{"subst"})
90+
err = cmd.Execute()
91+
92+
// Check output and error
93+
assert.NilError(t, err)
94+
assert.Equal(t, tc.output, outputBuf.String())
95+
})
96+
}
97+
}

cmd/policy/policy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ This group of commands allows the management of polices to be verified against b
9292
return nil
9393
},
9494
Args: cobra.ExactArgs(1),
95-
Example: `policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
95+
Example: `circleci policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
9696
}
9797

9898
cmd.Flags().StringVar(&context, "context", "config", "policy context")
@@ -127,7 +127,7 @@ This group of commands allows the management of polices to be verified against b
127127
return prettyJSONEncoder(cmd.OutOrStdout()).Encode(diff)
128128
},
129129
Args: cobra.ExactArgs(1),
130-
Example: `policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
130+
Example: `circleci policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
131131
}
132132
cmd.Flags().StringVar(&context, "context", "config", "policy context")
133133
cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
@@ -159,7 +159,7 @@ This group of commands allows the management of polices to be verified against b
159159
return nil
160160
},
161161
Args: cobra.MaximumNArgs(1),
162-
Example: `policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5`,
162+
Example: `circleci policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5`,
163163
}
164164

165165
cmd.Flags().StringVar(&context, "context", "config", "policy context")
@@ -238,7 +238,7 @@ This group of commands allows the management of polices to be verified against b
238238
return nil
239239
},
240240
Args: cobra.MaximumNArgs(1),
241-
Example: `policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json`,
241+
Example: `circleci policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json`,
242242
}
243243

244244
cmd.Flags().StringVar(&request.Status, "status", "", "filter decision logs based on their status")
@@ -313,7 +313,7 @@ This group of commands allows the management of polices to be verified against b
313313
return nil
314314
},
315315
Args: cobra.MaximumNArgs(1),
316-
Example: `policy decide ./policies --input ./.circleci/config.yml`,
316+
Example: `circleci policy decide ./policies --input ./.circleci/config.yml`,
317317
}
318318

319319
cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
@@ -359,7 +359,7 @@ This group of commands allows the management of polices to be verified against b
359359
return nil
360360
},
361361
Args: cobra.ExactArgs(1),
362-
Example: `policy eval ./policies --input ./.circleci/config.yml`,
362+
Example: `circleci policy eval ./policies --input ./.circleci/config.yml`,
363363
}
364364

365365
cmd.Flags().StringVar(&inputPath, "input", "", "path to input file")
@@ -406,7 +406,7 @@ This group of commands allows the management of polices to be verified against b
406406
return nil
407407
},
408408
Args: cobra.ExactArgs(0),
409-
Example: `policy settings --enabled=true`,
409+
Example: `circleci policy settings --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --enabled=true`,
410410
}
411411

412412
cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")

cmd/policy/testdata/policy/create-expected-usage.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)