11package commands
22
33import (
4- "encoding/json "
4+ "context "
55 "fmt"
6- "os/exec "
7- "strings "
6+ "sort "
7+ "time "
88
9+ "github.com/FredrikMWold/radix-tui/internal/radix"
910 tea "github.com/charmbracelet/bubbletea"
11+ "github.com/equinor/radix-cli/pkg/cache"
12+ radixconfig "github.com/equinor/radix-cli/pkg/config"
1013)
1114
1215type SelectedApplication string
@@ -19,18 +22,58 @@ func SelectApplication(application string) tea.Cmd {
1922
2023type Applications []string
2124
25+ // AuthWaiting indicates we expect to perform interactive login.
26+ type AuthWaiting struct {}
27+
28+ // AuthOK indicates cached provider/token is available; proceed to load apps.
29+ type AuthOK struct {}
30+
31+ // CheckAuth decides if we should show auth-waiting or start loading immediately.
32+ func CheckAuth () tea.Cmd {
33+ return func () tea.Msg {
34+ authCacheFilename := fmt .Sprintf ("%s/auth.json" , radixconfig .RadixConfigDir )
35+ global := cache .New (authCacheFilename , "global" )
36+ if prov , ok := global .GetItem ("auth_provider_type" ); ! ok || prov != "msal_interactive" {
37+ return AuthWaiting {}
38+ }
39+ // Check msal cache content under the interactive namespace
40+ msal := cache .New (authCacheFilename , "msal_interactive" )
41+ if content , ok := msal .GetItem ("msal" ); ! ok || len (content ) == 0 {
42+ return AuthWaiting {}
43+ }
44+ _ = time .Second // reserved for future TTL checks
45+ return AuthOK {}
46+ }
47+ }
48+
49+ // LoginInteractive triggers an interactive login, then signals AuthOK.
50+ type LoggedIn struct {}
51+
52+ func LoginInteractive () tea.Cmd {
53+ return func () tea.Msg {
54+ ctx := context .Background ()
55+ client , err := radix .New (false )
56+ if err == nil {
57+ _ = client .LoginInteractive (ctx )
58+ }
59+ return LoggedIn {}
60+ }
61+ }
62+
2263func GetApplications () tea.Msg {
23- result := exec . Command ( "rx" , "get" , "application" )
24- output , err := result . Output ( )
64+ ctx := context . Background ( )
65+ client , err := radix . New ( false )
2566 if err != nil {
2667 fmt .Println (err )
68+ return Applications {}
2769 }
28- trimmed := strings . TrimSpace ( string ( output ) )
29- application_list := strings . Split ( trimmed , " \n " )
30- if strings . Contains ( application_list [ 0 ], "login.microsoft" ) {
31- application_list = GetApplications ().( Applications )
70+ apps , err := client . ListApplications ( ctx )
71+ if err != nil {
72+ fmt . Println ( err )
73+ return Applications {}
3274 }
33- return Applications (application_list )
75+ sort .Strings (apps )
76+ return Applications (apps )
3477}
3578
3679type Application struct {
@@ -56,16 +99,63 @@ type Environment struct {
5699
57100func GetApplicationData (application string ) tea.Cmd {
58101 return func () tea.Msg {
59- result := exec . Command ( "rx" , "get" , "application" , "-a" , application )
60- output , err := result . Output ( )
102+ ctx := context . Background ( )
103+ client , err := radix . New ( false )
61104 if err != nil {
62105 fmt .Println (err )
106+ return Application {}
63107 }
64- var application Application
65- err = json .Unmarshal (output , & application )
108+ app , err := client .GetApplication (ctx , application )
66109 if err != nil {
67110 fmt .Println (err )
111+ return Application {}
112+ }
113+ // Map API model to local struct expected by views
114+ var jobs []Job
115+ if app .Jobs != nil {
116+ for _ , j := range app .Jobs {
117+ if j == nil {
118+ continue
119+ }
120+ jobs = append (jobs , Job {
121+ Name : stringDeref (j .Name ),
122+ TriggeredBy : j .TriggeredBy ,
123+ Environments : j .Environments ,
124+ Pipeline : j .Pipeline ,
125+ Status : j .Status ,
126+ Created : func () string {
127+ if j .Created != nil {
128+ return j .Created .String ()
129+ }
130+ return ""
131+ }(),
132+ })
133+ }
68134 }
69- return application
135+ var envs []Environment
136+ if app .Environments != nil {
137+ for _ , e := range app .Environments {
138+ if e == nil {
139+ continue
140+ }
141+ envs = append (envs , Environment {
142+ BranchMapping : e .BranchMapping ,
143+ Name : stringDeref (e .Name ),
144+ Status : e .Status ,
145+ })
146+ }
147+ }
148+ return Application {
149+ Jobs : jobs ,
150+ Environments : envs ,
151+ Name : stringDeref (app .Name ),
152+ }
153+ }
154+ }
155+
156+ func stringDeref (p * string ) string {
157+ if p == nil {
158+ return ""
70159 }
160+ return * p
71161}
0 commit comments