Skip to content

Commit 03ac21f

Browse files
authored
4638 Improve URL formatting (#695)
* Update internal code * Update commands code
1 parent 83b59fc commit 03ac21f

67 files changed

Lines changed: 457 additions & 145 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/kosli/allowArtifact.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
6+
"net/url"
77
"path/filepath"
88

99
"github.com/kosli-dev/cli/internal/requests"
@@ -82,7 +82,10 @@ func (o *allowArtifactOptions) run(args []string) error {
8282
}
8383
}
8484

85-
url := fmt.Sprintf("%s/api/v2/allowlists/%s/%s", global.Host, global.Org, o.environmentName)
85+
url, err := url.JoinPath(global.Host, "api/v2/allowlists", global.Org, o.environmentName)
86+
if err != nil {
87+
return err
88+
}
8689

8790
reqParams := &requests.RequestParams{
8891
Method: http.MethodPut,
@@ -91,7 +94,7 @@ func (o *allowArtifactOptions) run(args []string) error {
9194
DryRun: global.DryRun,
9295
Token: global.ApiToken,
9396
}
94-
_, err := kosliClient.Do(reqParams)
97+
_, err = kosliClient.Do(reqParams)
9598
if err == nil && !global.DryRun {
9699
logger.Info("artifact %s was allow listed in environment: %s", o.payload.Fingerprint, o.environmentName)
97100
}

cmd/kosli/archiveAttestationType.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
6+
"net/url"
77

88
"github.com/kosli-dev/cli/internal/requests"
99
"github.com/spf13/cobra"
@@ -38,15 +38,18 @@ func newArchiveAttestationTypeCmd(out io.Writer) *cobra.Command {
3838
return nil
3939
},
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
url := fmt.Sprintf("%s/api/v2/custom-attestation-types/%s/%s/archive", global.Host, global.Org, args[0])
41+
url, err := url.JoinPath(global.Host, "api/v2/custom-attestation-types", global.Org, args[0], "archive")
42+
if err != nil {
43+
return err
44+
}
4245

4346
reqParams := &requests.RequestParams{
4447
Method: http.MethodPut,
4548
URL: url,
4649
DryRun: global.DryRun,
4750
Token: global.ApiToken,
4851
}
49-
_, err := kosliClient.Do(reqParams)
52+
_, err = kosliClient.Do(reqParams)
5053
if err == nil && !global.DryRun {
5154
logger.Info("Custom attestation type %s was archived", args[0])
5255
}

cmd/kosli/archiveEnvironment.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
6+
"net/url"
77

88
"github.com/kosli-dev/cli/internal/requests"
99
"github.com/spf13/cobra"
@@ -38,15 +38,18 @@ func newArchiveEnvironmentCmd(out io.Writer) *cobra.Command {
3838
return nil
3939
},
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
url := fmt.Sprintf("%s/api/v2/environments/%s/%s/archive", global.Host, global.Org, args[0])
41+
url, err := url.JoinPath(global.Host, "api/v2/environments", global.Org, args[0], "archive")
42+
if err != nil {
43+
return err
44+
}
4245

4346
reqParams := &requests.RequestParams{
4447
Method: http.MethodPut,
4548
URL: url,
4649
DryRun: global.DryRun,
4750
Token: global.ApiToken,
4851
}
49-
_, err := kosliClient.Do(reqParams)
52+
_, err = kosliClient.Do(reqParams)
5053
if err == nil && !global.DryRun {
5154
logger.Info("environment %s was archived", args[0])
5255
}

cmd/kosli/archiveFlow.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
6+
"net/url"
77

88
"github.com/kosli-dev/cli/internal/requests"
99
"github.com/spf13/cobra"
@@ -38,15 +38,18 @@ func newArchiveFlowCmd(out io.Writer) *cobra.Command {
3838
return nil
3939
},
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
url := fmt.Sprintf("%s/api/v2/flows/%s/%s/archive", global.Host, global.Org, args[0])
41+
url, err := url.JoinPath(global.Host, "api/v2/flows", global.Org, args[0], "archive")
42+
if err != nil {
43+
return err
44+
}
4245

4346
reqParams := &requests.RequestParams{
4447
Method: http.MethodPut,
4548
URL: url,
4649
DryRun: global.DryRun,
4750
Token: global.ApiToken,
4851
}
49-
_, err := kosliClient.Do(reqParams)
52+
_, err = kosliClient.Do(reqParams)
5053
if err == nil && !global.DryRun {
5154
logger.Info("flow %s was archived", args[0])
5255
}

cmd/kosli/assertApproval.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"net/url"
89

910
"github.com/kosli-dev/cli/internal/requests"
1011
"github.com/spf13/cobra"
@@ -93,7 +94,10 @@ func (o *assertApprovalOptions) run(args []string) error {
9394
}
9495
}
9596

96-
url := fmt.Sprintf("%s/api/v2/artifacts/%s/%s/%s/approvals", global.Host, global.Org, o.flowName, o.fingerprint)
97+
url, err := url.JoinPath(global.Host, "api/v2/artifacts", global.Org, o.flowName, o.fingerprint, "approvals")
98+
if err != nil {
99+
return err
100+
}
97101

98102
reqParams := &requests.RequestParams{
99103
Method: http.MethodGet,

cmd/kosli/assertArtifact.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ func (o *assertArtifactOptions) run(out io.Writer, args []string) error {
117117
}
118118
}
119119

120-
baseURL := fmt.Sprintf("%s/api/v2/asserts/%s/fingerprint/%s", global.Host, global.Org, o.fingerprint)
120+
baseURL, err := url.JoinPath(global.Host, "api/v2/asserts", global.Org, "fingerprint", o.fingerprint)
121+
if err != nil {
122+
return err
123+
}
121124
params := url.Values{}
122125

123126
if o.flowName != "" {

cmd/kosli/assertSnapshot.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"net/url"
9+
"strconv"
810

911
"github.com/kosli-dev/cli/internal/requests"
1012
"github.com/spf13/cobra"
@@ -61,7 +63,10 @@ func run(out io.Writer, args []string) error {
6163
if err != nil {
6264
return err
6365
}
64-
url := fmt.Sprintf("%s/api/v2/snapshots/%s/%s/%d", global.Host, global.Org, envName, id)
66+
url, err := url.JoinPath(global.Host, "api/v2/snapshots", global.Org, envName, strconv.Itoa(id))
67+
if err != nil {
68+
return err
69+
}
6570

6671
reqParams := &requests.RequestParams{
6772
Method: http.MethodGet,

cmd/kosli/attachPolicy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"net/http"
6+
"net/url"
77

88
"github.com/kosli-dev/cli/internal/requests"
99
"github.com/spf13/cobra"
@@ -65,7 +65,10 @@ func newAttachPolicyCmd(out io.Writer) *cobra.Command {
6565
func (o *attachPolicyOptions) run(args []string) error {
6666
var err error
6767
for _, env := range o.environments {
68-
url := fmt.Sprintf("%s/api/v2/environments/%s/%s/policies", global.Host, global.Org, env)
68+
url, err := url.JoinPath(global.Host, "api/v2/environments", global.Org, env, "policies")
69+
if err != nil {
70+
return err
71+
}
6972
o.payload.PolicyNames = []string{args[0]}
7073
reqParams := &requests.RequestParams{
7174
Method: http.MethodPost,

cmd/kosli/attestArtifact.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"net/http"
7+
"net/url"
78
"path/filepath"
89

910
"github.com/kosli-dev/cli/internal/gitview"
@@ -201,7 +202,10 @@ func (o *attestArtifactOptions) run(args []string) error {
201202
logger.Warn("Repo URL will not be reported, %s", err.Error())
202203
}
203204

204-
url := fmt.Sprintf("%s/api/v2/artifacts/%s/%s", global.Host, global.Org, o.flowName)
205+
url, err := url.JoinPath(global.Host, "api/v2/artifacts", global.Org, o.flowName)
206+
if err != nil {
207+
return err
208+
}
205209

206210
reqParams := &requests.RequestParams{
207211
Method: http.MethodPost,

cmd/kosli/attestCustom.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"net/http"
7+
"net/url"
78
"os"
89

910
"github.com/kosli-dev/cli/internal/requests"
@@ -152,9 +153,12 @@ func newAttestCustomCmd(out io.Writer) *cobra.Command {
152153
}
153154

154155
func (o *attestCustomOptions) run(args []string) error {
155-
url := fmt.Sprintf("%s/api/v2/attestations/%s/%s/trail/%s/custom", global.Host, global.Org, o.flowName, o.trailName)
156+
url, err := url.JoinPath(global.Host, "api/v2/attestations", global.Org, o.flowName, "trail", o.trailName, "custom")
157+
if err != nil {
158+
return err
159+
}
156160

157-
err := o.CommonAttestationOptions.run(args, o.payload.CommonAttestationPayload)
161+
err = o.CommonAttestationOptions.run(args, o.payload.CommonAttestationPayload)
158162
if err != nil {
159163
return err
160164
}

0 commit comments

Comments
 (0)