@@ -37,6 +37,9 @@ type PublishCommand struct {
37
37
statusCheckOff bool
38
38
statusCheckPath string
39
39
statusCheckTimeout int
40
+
41
+ // Publish private fields
42
+ projectDir string
40
43
}
41
44
42
45
// NewPublishCommand returns a usable command registered under the parent.
@@ -93,6 +96,42 @@ func NewPublishCommand(parent argparser.Registerer, g *global.Data, build *Build
93
96
// non-deterministic ways. It's best to leave those nested commands to handle
94
97
// the progress indicator.
95
98
func (c * PublishCommand ) Exec (in io.Reader , out io.Writer ) (err error ) {
99
+ wd , err := os .Getwd ()
100
+ if err != nil {
101
+ return fmt .Errorf ("failed to get current working directory: %w" , err )
102
+ }
103
+ defer func () {
104
+ _ = os .Chdir (wd )
105
+ }()
106
+
107
+ c .projectDir , err = ChangeProjectDirectory (c .dir .Value )
108
+ if err != nil {
109
+ return err
110
+ }
111
+ if c .projectDir != "" {
112
+ if c .Globals .Verbose () {
113
+ text .Info (out , ProjectDirMsg , c .projectDir )
114
+ }
115
+ }
116
+
117
+ err = c .Build (in , out )
118
+ if err != nil {
119
+ c .Globals .ErrLog .Add (err )
120
+ return err
121
+ }
122
+
123
+ text .Break (out )
124
+
125
+ err = c .Deploy (in , out )
126
+ if err != nil {
127
+ c .Globals .ErrLog .Add (err )
128
+ return err
129
+ }
130
+ return nil
131
+ }
132
+
133
+ // Build constructs and executes the build logic.
134
+ func (c * PublishCommand ) Build (in io.Reader , out io.Writer ) error {
96
135
// Reset the fields on the BuildCommand based on PublishCommand values.
97
136
if c .dir .WasSet {
98
137
c .build .Flags .Dir = c .dir .Value
@@ -121,33 +160,14 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
121
160
if c .metadataShow .WasSet {
122
161
c .build .MetadataShow = c .metadataShow .Value
123
162
}
124
-
125
- err = c .build .Exec (in , out )
126
- if err != nil {
127
- c .Globals .ErrLog .Add (err )
128
- return err
129
- }
130
-
131
- text .Break (out )
132
-
133
- wd , err := os .Getwd ()
134
- if err != nil {
135
- return fmt .Errorf ("failed to get current working directory: %w" , err )
136
- }
137
- defer func () {
138
- _ = os .Chdir (wd )
139
- }()
140
-
141
- projectDir , err := ChangeProjectDirectory (c .dir .Value )
142
- if err != nil {
143
- return err
144
- }
145
- if projectDir != "" {
146
- if c .Globals .Verbose () {
147
- text .Info (out , ProjectDirMsg , projectDir )
148
- }
163
+ if c .projectDir != "" {
164
+ c .build .SkipChangeDir = true // we've already changed directory
149
165
}
166
+ return c .build .Exec (in , out )
167
+ }
150
168
169
+ // Deploy constructs and executes the deploy logic.
170
+ func (c * PublishCommand ) Deploy (in io.Reader , out io.Writer ) error {
151
171
// Reset the fields on the DeployCommand based on PublishCommand values.
152
172
if c .dir .WasSet {
153
173
c .deploy .Dir = c .dir .Value
@@ -180,12 +200,5 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
180
200
c .deploy .StatusCheckTimeout = c .statusCheckTimeout
181
201
}
182
202
c .deploy .StatusCheckPath = c .statusCheckPath
183
-
184
- err = c .deploy .Exec (in , out )
185
- if err != nil {
186
- c .Globals .ErrLog .Add (err )
187
- return err
188
- }
189
-
190
- return nil
203
+ return c .deploy .Exec (in , out )
191
204
}
0 commit comments