@@ -59,6 +59,8 @@ const (
5959 // The stacks plugin release download service that the BinaryManager relies
6060 // on to fetch the plugin.
6161 stackspluginServiceID = "stacksplugin.v1"
62+
63+ defaultHostname = "app.terraform.io"
6264)
6365
6466var (
@@ -165,20 +167,8 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
165167
166168 displayHostname := os .Getenv ("TF_STACKS_HOSTNAME" )
167169 if strings .TrimSpace (displayHostname ) == "" {
168- return diags .Append (tfdiags .Sourceless (
169- tfdiags .Error ,
170- "TF_STACKS_HOSTNAME is not set" ,
171- "TF_STACKS_HOSTNAME must be set to the hostname of the HCP Terraform instance" ,
172- ))
173- }
174-
175- token := os .Getenv ("TF_STACKS_TOKEN" )
176- if strings .TrimSpace (token ) == "" {
177- return diags .Append (tfdiags .Sourceless (
178- tfdiags .Error ,
179- "TF_STACKS_TOKEN is not set" ,
180- "TF_STACKS_TOKEN must be set to the token of the HCP Terraform instance" ,
181- ))
170+ log .Printf ("[TRACE] stacksplugin hostname not set, falling back to %q" , defaultHostname )
171+ displayHostname = defaultHostname
182172 }
183173
184174 hostname , err := svchost .ForComparison (displayHostname )
@@ -208,6 +198,20 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
208198 // The discovery request worked, so cache the full results.
209199 cb .ServicesHost = host
210200
201+ token := os .Getenv ("TF_STACKS_TOKEN" )
202+ if strings .TrimSpace (token ) == "" {
203+ // attempt to read from the credentials file
204+ token , err = cloud .CliConfigToken (hostname , cb .Services ())
205+ if err != nil {
206+ // some commands like stacks init and validate could be run without a token so allow it without errors
207+ diags .Append (tfdiags .Sourceless (
208+ tfdiags .Warning ,
209+ "Could not read token from credentials file, proceeding without a token" ,
210+ err .Error (),
211+ ))
212+ }
213+ }
214+
211215 // re-use the cached service discovery info for this TFC
212216 // instance to find our plugin service and TFE API URLs:
213217 pluginService , err := cb .ServicesHost .ServiceURL (stackspluginServiceID )
@@ -244,6 +248,7 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
244248 OrganizationName : orgName ,
245249 ProjectName : projectName ,
246250 StackName : stackName ,
251+ TerminalWidth : c .Meta .Streams .Stdout .Columns (),
247252 }
248253
249254 return diags
@@ -346,6 +351,7 @@ type StacksPluginConfig struct {
346351 OrganizationName string `md:"tfc-organization"`
347352 ProjectName string `md:"tfc-project"`
348353 StackName string `md:"tfc-stack"`
354+ TerminalWidth int `md:"terminal-width"`
349355}
350356
351357func (c StacksPluginConfig ) ToMetadata () metadata.MD {
@@ -358,6 +364,7 @@ func (c StacksPluginConfig) ToMetadata() metadata.MD {
358364 "tfc-organization" , c .OrganizationName ,
359365 "tfc-project" , c .ProjectName ,
360366 "tfc-stack" , c .StackName ,
367+ "terminal-width" , fmt .Sprintf ("%d" , c .TerminalWidth ),
361368 )
362369 return md
363370}
0 commit comments