6
6
"sort"
7
7
"strings"
8
8
"text/tabwriter"
9
+ "time"
9
10
10
11
cmd2 "github.com/acorn-io/cmd"
11
12
"github.com/gptscript-ai/gptscript/pkg/cache"
@@ -46,6 +47,7 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
46
47
}
47
48
opts .Cache = cache .Complete (opts .Cache )
48
49
50
+ // Initialize the credential store and get all the credentials.
49
51
store , err := credentials .NewStore (cfg , ctx , opts .Cache .CacheDir )
50
52
if err != nil {
51
53
return fmt .Errorf ("failed to get credentials store: %w" , err )
@@ -56,6 +58,10 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
56
58
return fmt .Errorf ("failed to list credentials: %w" , err )
57
59
}
58
60
61
+ w := tabwriter .NewWriter (os .Stdout , 10 , 1 , 3 , ' ' , 0 )
62
+ defer w .Flush ()
63
+
64
+ // Sort credentials and print column names, depending on the options.
59
65
if c .AllContexts {
60
66
// Sort credentials by context
61
67
sort .Slice (creds , func (i , j int ) bool {
@@ -65,25 +71,10 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
65
71
return creds [i ].Context < creds [j ].Context
66
72
})
67
73
68
- w := tabwriter .NewWriter (os .Stdout , 10 , 1 , 3 , ' ' , 0 )
69
- defer w .Flush ()
70
-
71
74
if c .ShowEnvVars {
72
- _ , _ = w .Write ([]byte ("CONTEXT\t CREDENTIAL\t ENVIRONMENT VARIABLES\n " ))
73
-
74
- for _ , cred := range creds {
75
- envVars := make ([]string , 0 , len (cred .Env ))
76
- for envVar := range cred .Env {
77
- envVars = append (envVars , envVar )
78
- }
79
- sort .Strings (envVars )
80
- _ , _ = fmt .Fprintf (w , "%s\t %s\t %s\n " , cred .Context , cred .ToolName , strings .Join (envVars , ", " ))
81
- }
75
+ _ , _ = w .Write ([]byte ("CONTEXT\t CREDENTIAL\t EXPIRES IN\t ENV\n " ))
82
76
} else {
83
- _ , _ = w .Write ([]byte ("CONTEXT\t CREDENTIAL\n " ))
84
- for _ , cred := range creds {
85
- _ , _ = fmt .Fprintf (w , "%s\t %s\n " , cred .Context , cred .ToolName )
86
- }
77
+ _ , _ = w .Write ([]byte ("CONTEXT\t CREDENTIAL\t EXPIRES IN\n " ))
87
78
}
88
79
} else {
89
80
// Sort credentials by tool name
@@ -92,24 +83,49 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
92
83
})
93
84
94
85
if c .ShowEnvVars {
95
- w := tabwriter .NewWriter (os .Stdout , 10 , 1 , 3 , ' ' , 0 )
96
- defer w .Flush ()
97
- _ , _ = w .Write ([]byte ("CREDENTIAL\t ENVIRONMENT VARIABLES\n " ))
98
-
99
- for _ , cred := range creds {
100
- envVars := make ([]string , 0 , len (cred .Env ))
101
- for envVar := range cred .Env {
102
- envVars = append (envVars , envVar )
103
- }
104
- sort .Strings (envVars )
105
- _ , _ = fmt .Fprintf (w , "%s\t %s\n " , cred .ToolName , strings .Join (envVars , ", " ))
86
+ _ , _ = w .Write ([]byte ("CREDENTIAL\t EXPIRES IN\t ENV\n " ))
87
+ } else {
88
+ _ , _ = w .Write ([]byte ("CREDENTIAL\t EXPIRES IN\n " ))
89
+ }
90
+ }
91
+
92
+ for _ , cred := range creds {
93
+ expires := "never"
94
+ if cred .ExpiresAt != nil {
95
+ if ! cred .IsExpired () {
96
+ expires = cred .ExpiresAt .Sub (time .Now ()).Truncate (time .Second ).String ()
97
+ } else {
98
+ expires = "expired"
106
99
}
100
+ }
101
+
102
+ var fields []any
103
+ if c .AllContexts {
104
+ fields = []any {cred .Context , cred .ToolName , expires }
107
105
} else {
108
- for _ , cred := range creds {
109
- fmt .Println (cred .ToolName )
106
+ fields = []any {cred .ToolName , expires }
107
+ }
108
+
109
+ if c .ShowEnvVars {
110
+ envVars := make ([]string , 0 , len (cred .Env ))
111
+ for envVar := range cred .Env {
112
+ envVars = append (envVars , envVar )
110
113
}
114
+ sort .Strings (envVars )
115
+ fields = append (fields , strings .Join (envVars , ", " ))
111
116
}
117
+
118
+ printFields (w , fields )
112
119
}
113
120
114
121
return nil
115
122
}
123
+
124
+ func printFields (w * tabwriter.Writer , fields []any ) {
125
+ if len (fields ) == 0 {
126
+ return
127
+ }
128
+
129
+ fmtStr := strings .Repeat ("%s\t " , len (fields )- 1 ) + "%s\n "
130
+ _ , _ = fmt .Fprintf (w , fmtStr , fields ... )
131
+ }
0 commit comments