@@ -4,13 +4,13 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- "os"
8
- "os/signal"
9
- "syscall"
10
- secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
11
7
"github.com/docker/labs-ai-tools-for-devs/pkg/client"
8
+ secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
12
9
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
13
10
"github.com/spf13/cobra"
11
+ "os"
12
+ "os/signal"
13
+ "syscall"
14
14
)
15
15
16
16
func main () {
@@ -20,6 +20,9 @@ func main() {
20
20
cmd := AddSecret (ctx )
21
21
cmd .AddCommand (ListSecrets (ctx ))
22
22
cmd .AddCommand (DeleteSecret (ctx ))
23
+ cmd .AddCommand (AuthorizeApp (ctx ))
24
+ cmd .AddCommand (UnauthorizeApp (ctx ))
25
+ cmd .AddCommand (ListOAuthApps (ctx ))
23
26
if err := cmd .Execute (); err != nil {
24
27
fmt .Println (err )
25
28
os .Exit (1 )
@@ -45,6 +48,105 @@ func newApiClient() (client.ApiClient, error) {
45
48
return client .NewApiClient (p ), nil
46
49
}
47
50
51
+ func newOAuthApiClient () (client.OAuthApiClient , error ) {
52
+ p , err := paths .GetToolsApiSocketPath ()
53
+ if err != nil {
54
+ return nil , err
55
+ }
56
+ return client .NewOAuthApiClient (p ), nil
57
+ }
58
+
59
+ func ListOAuthApps (ctx context.Context ) * cobra.Command {
60
+ cmd := & cobra.Command {
61
+ Use : "list-oauth-apps" ,
62
+ Short : "List all OAuth apps" ,
63
+ Args : cobra .NoArgs ,
64
+ RunE : func (* cobra.Command , []string ) error {
65
+ return runListOAuthApps (ctx )
66
+ },
67
+ }
68
+ return cmd
69
+ }
70
+
71
+ func runListOAuthApps (ctx context.Context ) error {
72
+ c , err := newOAuthApiClient ()
73
+ if err != nil {
74
+ return err
75
+ }
76
+ apps , err := c .ListOAuthApps (ctx )
77
+ if err != nil {
78
+ return err
79
+ }
80
+ return json .NewEncoder (os .Stdout ).Encode (apps )
81
+ }
82
+
83
+ type authorizeOptions struct {
84
+ Name string
85
+ Scopes string
86
+ }
87
+
88
+ func AuthorizeApp (ctx context.Context ) * cobra.Command {
89
+ opts := & authorizeOptions {}
90
+ cmd := & cobra.Command {
91
+ Use : "authorize" ,
92
+ Short : "Authorize an OAuth app" ,
93
+ Args : cobra .NoArgs ,
94
+ RunE : func (* cobra.Command , []string ) error {
95
+ return runAuthorizeApp (ctx , * opts )
96
+ },
97
+ }
98
+ flags := cmd .Flags ()
99
+ flags .StringVarP (& opts .Name , "name" , "n" , "" , "Name of the OAuth app" )
100
+ _ = cmd .MarkFlagRequired ("name" )
101
+ flags .StringVarP (& opts .Scopes , "scopes" , "s" , "" , "Scopes for the OAuth app" )
102
+ return cmd
103
+ }
104
+
105
+ func runAuthorizeApp (ctx context.Context , opts authorizeOptions ) error {
106
+ c , err := newOAuthApiClient ()
107
+ if err != nil {
108
+ return err
109
+ }
110
+ authResponse , err := c .PostOAuthApp (ctx , opts .Name , opts .Scopes )
111
+ if err != nil {
112
+ return err
113
+ }
114
+ return json .NewEncoder (os .Stdout ).Encode (authResponse )
115
+ }
116
+
117
+ type unauthorizeOptions struct {
118
+ Name string
119
+ }
120
+
121
+ func UnauthorizeApp (ctx context.Context ) * cobra.Command {
122
+ opts := & unauthorizeOptions {}
123
+ cmd := & cobra.Command {
124
+ Use : "unauthorize" ,
125
+ Short : "Unauthorize an OAuth app" ,
126
+ Args : cobra .NoArgs ,
127
+ RunE : func (* cobra.Command , []string ) error {
128
+ return runUnauthorizeApp (ctx , * opts )
129
+ },
130
+ }
131
+ flags := cmd .Flags ()
132
+ flags .StringVarP (& opts .Name , "name" , "n" , "" , "Name of the OAuth app" )
133
+ _ = cmd .MarkFlagRequired ("name" )
134
+ return cmd
135
+ }
136
+
137
+ func runUnauthorizeApp (ctx context.Context , opts unauthorizeOptions ) error {
138
+ c , err := newOAuthApiClient ()
139
+ if err != nil {
140
+ return err
141
+ }
142
+ err = c .DeleteOAuthApp (ctx , opts .Name )
143
+ if err != nil {
144
+ return err
145
+ }
146
+ fmt .Printf ("App %s has been unauthorised\n " , opts .Name )
147
+ return nil
148
+ }
149
+
48
150
type addOptions struct {
49
151
Name string
50
152
Value string
@@ -87,7 +189,7 @@ func ListSecrets(ctx context.Context) *cobra.Command {
87
189
func DeleteSecret (ctx context.Context ) * cobra.Command {
88
190
opts := & deleteOptions {}
89
191
cmd := & cobra.Command {
90
- Use : "delete" ,
192
+ Use : "delete" ,
91
193
Short : "Delete a secret" ,
92
194
Args : cobra .NoArgs ,
93
195
RunE : func (* cobra.Command , []string ) error {
0 commit comments