@@ -7,23 +7,26 @@ import (
77
88 "github.com/pkg/errors"
99 "github.com/pulumi/pulumi/sdk/v3/go/auto"
10+ "github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
11+ "github.com/pulumi/pulumi/sdk/v3/go/auto/optrefresh"
1012 "github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
1113 "go.uber.org/multierr"
1214 "go.uber.org/zap"
1315)
1416
1517type VictorArgs struct {
16- Verbose bool
17- Version string
18- Statefile string
19- Username * string
20- Password * string
21- Passphrase string
22- Context string
23- Server * string
24- Resources []string
25- Configuration []string
26- Outputs * string
18+ Verbose bool
19+ Version string
20+ Statefile string
21+ Username * string
22+ Password * string
23+ Passphrase string
24+ Context string
25+ Server * string
26+ Resources []string
27+ Configuration []string
28+ Outputs * string
29+ DestroyIfNonEmpty bool
2730}
2831
2932func Victor (ctx context.Context , args * VictorArgs ) error {
@@ -34,8 +37,8 @@ func Victor(ctx context.Context, args *VictorArgs) error {
3437 }
3538 logger := Log ()
3639
37- // Build Victor's client
38- client := NewClient (args .Version , args .Verbose , args .Username , args .Password )
40+ // Build Victor's cli
41+ cli := NewClient (args .Version , args .Verbose , args .Username , args .Password )
3942
4043 // Create the workspace
4144 if args .Verbose {
@@ -69,7 +72,7 @@ func Victor(ctx context.Context, args *VictorArgs) error {
6972 if args .Verbose {
7073 logger .Info ("getting the stack" )
7174 }
72- stack , err := GetStack (ctx , client , ws , args .Statefile , args .Verbose )
75+ stack , err := GetStack (ctx , cli , ws , args .Statefile , args .Verbose )
7376 if err != nil {
7477 return err
7578 }
@@ -86,35 +89,44 @@ func Victor(ctx context.Context, args *VictorArgs) error {
8689 }
8790
8891 // Refresh and update
89- upopts := []optup.Option {}
92+ upopts := []optup.Option {
93+ optup .UserAgent (cli .UserAgent ()),
94+ }
9095 if args .Verbose {
9196 logger .Info ("refreshing and updating Pulumi stack" )
9297 zw := & ZapWriter {logger : logger }
9398 upopts = append (upopts , optup .ProgressStreams (zw ))
9499 }
95- _ , err = stack .Refresh (ctx )
100+
101+ // Make sure to properly track resources and changes
102+ refRes , err := stack .Refresh (ctx , optrefresh .UserAgent (cli .UserAgent ()))
96103 if err != nil {
97104 return errors .Wrap (err , "refreshing Pulumi stack" )
98105 }
106+ if refRes .Summary .ResourceChanges != nil && len (* refRes .Summary .ResourceChanges ) != 0 {
107+ if _ , err := stack .Destroy (ctx , optdestroy .UserAgent (cli .UserAgent ())); err != nil {
108+ return errors .Wrap (err , "destroying Pulumi stack ahead of update" )
109+ }
110+ }
111+
112+ // Up(date) the stack
99113 res , err := stack .Up (ctx , upopts ... )
100114 if err != nil {
101115 return errors .Wrap (err , "stack up" )
102116 }
103117
104118 // Export outputs
105- if args .Outputs != nil {
106- if err := ExportOutputs (res .Outputs , * args .Outputs ); err != nil {
107- logger .Error ("exporting outputs" ,
108- zap .Error (err ),
109- )
110- }
119+ if err := args .Export (res .Outputs ); err != nil {
120+ logger .Error ("exporting outputs" ,
121+ zap .Error (err ),
122+ )
111123 }
112124
113125 // Export stack
114126 if args .Verbose {
115- logger .Info ("pushing the stack" )
127+ logger .Info ("pushing stack" )
116128 }
117- return PushStack (ctx , client , stack , args .Statefile )
129+ return PushStack (ctx , cli , stack , args .Statefile )
118130}
119131
120132type ZapWriter struct {
0 commit comments