@@ -15,17 +15,41 @@ import (
1515 platform "github.com/tonedefdev/terracreds/pkg/platform"
1616)
1717
18- // terracreds interface implements these methods
19- type terracreds interface {
18+ // Terracreds interface implements these methods for a credential's lifecycle
19+ type Terracreds interface {
20+ // Create or store an API token in a vault
2021 Create (cfg api.Config , hostname string , token interface {}, user * user.User )
22+ // Delete or forget an API token in a vault
2123 Delete (cfg api.Config , command string , hostname string , user * user.User )
24+ // Get or retrieve an API token in a vault
2225 Get (cfg api.Config , hostname string , user * user.User )
2326}
2427
28+ func returnProvider (os string ) Terracreds {
29+ switch os {
30+ case "darwin" :
31+ return platform.Mac {}
32+ case "linux" :
33+ return platform.Linux {}
34+ case "windows" :
35+ return platform.Windows {}
36+ default :
37+ return nil
38+ }
39+ }
40+
41+ var Provider Terracreds
42+
2543func main () {
2644 var cfg api.Config
2745 version := "1.1.2"
2846 helpers .LoadConfig (& cfg )
47+
48+ provider := returnProvider (runtime .GOOS )
49+ if provider == nil {
50+ fmt .Fprintf (color .Output , "%s: Terracreds cannot run on this platform: '%s'\n " , color .RedString ("ERROR" ), runtime .GOOS )
51+ }
52+
2953 app := & cli.App {
3054 Name : "terracreds" ,
3155 Usage : "a credential helper for Terraform Cloud/Enterprise that leverages the local operating system's credential manager for securely storing your API tokens.\n \n Visit https://github.com/tonedefdev/terracreds for more information" ,
@@ -55,18 +79,7 @@ func main() {
5579 } else {
5680 user , err := user .Current ()
5781 helpers .CheckError (err )
58-
59- if runtime .GOOS == "windows" {
60- terracreds .Create (platform.Windows {}, cfg , c .String ("hostname" ), c .String ("apiToken" ), user )
61- }
62-
63- if runtime .GOOS == "dawrin" {
64- terracreds .Create (platform.Mac {}, cfg , c .String ("hostname" ), c .String ("apiToken" ), user )
65- }
66-
67- if runtime .GOOS == "linux" {
68- terracreds .Create (platform.Linux {}, cfg , c .String ("hostname" ), c .String ("apiToken" ), user )
69- }
82+ Terracreds .Create (provider , cfg , c .String ("hostname" ), c .String ("apiToken" ), user )
7083 }
7184 return nil
7285 },
@@ -92,18 +105,7 @@ func main() {
92105 } else {
93106 user , err := user .Current ()
94107 helpers .CheckError (err )
95-
96- if runtime .GOOS == "windows" {
97- terracreds .Delete (platform.Windows {}, cfg , os .Args [1 ], c .String ("hostname" ), user )
98- }
99-
100- if runtime .GOOS == "dawrin" {
101- terracreds .Delete (platform.Mac {}, cfg , os .Args [1 ], c .String ("hostname" ), user )
102- }
103-
104- if runtime .GOOS == "linux" {
105- terracreds .Delete (platform.Linux {}, cfg , os .Args [1 ], c .String ("hostname" ), user )
106- }
108+ Terracreds .Delete (provider , cfg , os .Args [1 ], c .String ("hostname" ), user )
107109 }
108110 return nil
109111 },
@@ -117,18 +119,7 @@ func main() {
117119 } else {
118120 user , err := user .Current ()
119121 helpers .CheckError (err )
120-
121- if runtime .GOOS == "windows" {
122- terracreds .Delete (platform.Windows {}, cfg , os .Args [1 ], os .Args [2 ], user )
123- }
124-
125- if runtime .GOOS == "dawrin" {
126- terracreds .Delete (platform.Mac {}, cfg , os .Args [1 ], os .Args [2 ], user )
127- }
128-
129- if runtime .GOOS == "linux" {
130- terracreds .Delete (platform.Linux {}, cfg , os .Args [1 ], os .Args [2 ], user )
131- }
122+ Terracreds .Delete (provider , cfg , os .Args [1 ], os .Args [2 ], user )
132123 }
133124 return nil
134125 },
@@ -155,18 +146,7 @@ func main() {
155146 if len (os .Args ) > 2 {
156147 user , err := user .Current ()
157148 helpers .CheckError (err )
158-
159- if runtime .GOOS == "windows" {
160- terracreds .Get (platform.Windows {}, cfg , os .Args [2 ], user )
161- }
162-
163- if runtime .GOOS == "dawrin" {
164- terracreds .Get (platform.Mac {}, cfg , os .Args [2 ], user )
165- }
166-
167- if runtime .GOOS == "linux" {
168- terracreds .Get (platform.Linux {}, cfg , os .Args [2 ], user )
169- }
149+ Terracreds .Get (provider , cfg , os .Args [2 ], user )
170150 } else {
171151 msg := "- hostname was expected after the 'get' command but no argument was provided"
172152 helpers .Logging (cfg , msg , "ERROR" )
@@ -184,18 +164,7 @@ func main() {
184164 } else {
185165 user , err := user .Current ()
186166 helpers .CheckError (err )
187-
188- if runtime .GOOS == "windows" {
189- terracreds .Create (platform.Windows {}, cfg , os .Args [2 ], nil , user )
190- }
191-
192- if runtime .GOOS == "dawrin" {
193- terracreds .Create (platform.Mac {}, cfg , os .Args [2 ], nil , user )
194- }
195-
196- if runtime .GOOS == "linux" {
197- terracreds .Create (platform.Linux {}, cfg , os .Args [2 ], nil , user )
198- }
167+ Terracreds .Create (provider , cfg , os .Args [2 ], nil , user )
199168 }
200169 return nil
201170 },
0 commit comments