11package internal
22
33import (
4- "encoding/json"
5- "flag"
6- "fmt"
7- "os"
8- "time"
9- "strings"
4+ "encoding/json"
5+ "errors"
6+ "flag"
7+ "fmt"
8+ "os"
9+ "time"
10+ "github.com/privapps/github-copilot-svcs/pkg/transform"
1011)
1112
1213// Command constants to avoid goconst errors
@@ -112,14 +113,14 @@ func handleAuth() error {
112113}
113114
114115func handleStatusWithFormat (jsonOutput bool ) error {
115- cfg , err := LoadConfig ()
116- if err != nil {
117- if strings . Contains ( err . Error (), "either github_token or copilot_token must be provided" ) {
118- fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
119- return nil
120- }
121- return fmt .Errorf ("failed to load config: %v" , err )
122- }
116+ cfg , err := LoadConfig ()
117+ if err != nil {
118+ if errors . Is ( err , ErrMissingTokens ) {
119+ fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
120+ return nil
121+ }
122+ return fmt .Errorf ("failed to load config: %v" , err )
123+ }
123124
124125 if jsonOutput {
125126 return printStatusJSON (cfg )
@@ -213,14 +214,14 @@ func printStatusText(cfg *Config) error {
213214}
214215
215216func handleConfig () error {
216- cfg , err := LoadConfig ()
217- if err != nil {
218- if strings . Contains ( err . Error (), "either github_token or copilot_token must be provided" ) {
219- fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
220- return nil
221- }
222- return fmt .Errorf ("failed to load config: %v" , err )
223- }
217+ cfg , err := LoadConfig ()
218+ if err != nil {
219+ if errors . Is ( err , ErrMissingTokens ) {
220+ fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
221+ return nil
222+ }
223+ return fmt .Errorf ("failed to load config: %v" , err )
224+ }
224225
225226 path , _ := GetConfigPath ()
226227 fmt .Printf ("Configuration file: %s\n " , path )
@@ -248,20 +249,20 @@ func getCurrentTime() int64 {
248249}
249250
250251func handleRun () error {
251- cfg , err := LoadConfig ()
252- if err != nil {
253- if strings . Contains ( err . Error (), "either github_token or copilot_token must be provided" ) {
254- if authErr := handleAuth (); authErr != nil {
255- return fmt .Errorf ("authentication failed: %v" , authErr )
256- }
257- cfg , err = LoadConfig ()
258- if err != nil {
259- return fmt .Errorf ("failed to load config after authentication: %v" , err )
260- }
261- } else {
262- return fmt .Errorf ("failed to load config: %v" , err )
263- }
264- }
252+ cfg , err := LoadConfig ()
253+ if err != nil {
254+ if errors . Is ( err , ErrMissingTokens ) {
255+ if authErr := handleAuth (); authErr != nil {
256+ return fmt .Errorf ("authentication failed: %v" , authErr )
257+ }
258+ cfg , err = LoadConfig ()
259+ if err != nil {
260+ return fmt .Errorf ("failed to load config after authentication: %v" , err )
261+ }
262+ } else {
263+ return fmt .Errorf ("failed to load config: %v" , err )
264+ }
265+ }
265266
266267 // Create HTTP client and auth service
267268 httpClient := CreateHTTPClient (cfg )
@@ -278,14 +279,14 @@ func handleRun() error {
278279}
279280
280281func handleModels () error {
281- cfg , err := LoadConfig ()
282- if err != nil {
283- if strings . Contains ( err . Error (), "either github_token or copilot_token must be provided" ) {
284- fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
285- return nil
286- }
287- return fmt .Errorf ("failed to load config: %v" , err )
288- }
282+ cfg , err := LoadConfig ()
283+ if err != nil {
284+ if errors . Is ( err , ErrMissingTokens ) {
285+ fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
286+ return nil
287+ }
288+ return fmt .Errorf ("failed to load config: %v" , err )
289+ }
289290
290291 // Create HTTP client and auth service
291292 httpClient := CreateHTTPClient (cfg )
@@ -308,23 +309,52 @@ func handleModels() error {
308309 return nil
309310 }
310311
311- fmt .Printf ("Available models (%d total):\n " , len (modelList .Data ))
312- for _ , model := range modelList .Data {
313- fmt .Printf (" - %s (%s)\n " , model .ID , model .OwnedBy )
314- }
315-
316- return nil
317- }
312+ filtered := modelList .Data
313+ var unknown []string
314+ filteredMsg := ""
315+ if len (cfg .AllowedModels ) > 0 {
316+ allowedSet := make (map [string ]struct {}, len (cfg .AllowedModels ))
317+ for _ , name := range cfg .AllowedModels {
318+ allowedSet [name ] = struct {}{}
319+ }
320+ var tmp []transform.Model
321+ foundSet := make (map [string ]struct {})
322+ for _ , model := range filtered {
323+ if _ , ok := allowedSet [model .ID ]; ok {
324+ tmp = append (tmp , model )
325+ foundSet [model .ID ] = struct {}{}
326+ }
327+ }
328+ for k := range allowedSet {
329+ if _ , ok := foundSet [k ]; ! ok {
330+ unknown = append (unknown , k )
331+ }
332+ }
333+ filtered = tmp
334+ filteredMsg = "NOTE: The model list is filtered by allowed_models in config."
335+ if len (unknown ) > 0 {
336+ fmt .Printf ("WARNING: The following allowed_models were not found and are ignored: %v\n " , unknown )
337+ }
338+ }
339+ fmt .Printf ("Available models (%d shown):\n " , len (filtered ))
340+ for _ , model := range filtered {
341+ fmt .Printf (" - %s (%s)\n " , model .ID , model .OwnedBy )
342+ }
343+ if filteredMsg != "" {
344+ fmt .Println (filteredMsg )
345+ }
346+ return nil
347+ }
318348
319349func handleRefresh () error {
320- cfg , err := LoadConfig ()
321- if err != nil {
322- if strings . Contains ( err . Error (), "either github_token or copilot_token must be provided" ) {
323- fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
324- return nil
325- }
326- return fmt .Errorf ("failed to load config: %v" , err )
327- }
350+ cfg , err := LoadConfig ()
351+ if err != nil {
352+ if errors . Is ( err , ErrMissingTokens ) {
353+ fmt .Println ("Not authenticated. Run 'auth' to authenticate." )
354+ return nil
355+ }
356+ return fmt .Errorf ("failed to load config: %v" , err )
357+ }
328358
329359 if cfg .CopilotToken == "" {
330360 return fmt .Errorf ("no token to refresh - run 'auth' command first" )
0 commit comments