Skip to content

Commit a616ed1

Browse files
ChristopherHXtechknowlogick
authored andcommitted
feat: register interactive with values from cli (#682)
I used to be able to do something like `./act_runner register --instance https://gitea.com --token testdcff --name test` on GitHub Actions Runners, but act_runner always asked me to enter instance, token etc. again and requiring me to use `--no-interactive` including passing everything per cli. My idea was to extract the preset input of some stages to skip the prompt for this value if it is a non empty string. Labels is the only question that has been asked more than once if validation failed, in this case the error path have to unset the values of the input structure to not end in a non-interactive loop. _I have written this initially for my own gitea runner, might be useful to everyone using the official runner as well_ Reviewed-on: https://gitea.com/gitea/act_runner/pulls/682 Reviewed-by: Lunny Xiao <[email protected]> Co-authored-by: Christopher Homberger <[email protected]> Co-committed-by: Christopher Homberger <[email protected]>
1 parent f0b5aff commit a616ed1

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

internal/app/cmd/register.go

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string)
5252
}
5353
} else {
5454
go func() {
55-
if err := registerInteractive(ctx, *configFile); err != nil {
55+
if err := registerInteractive(ctx, *configFile, regArgs); err != nil {
5656
log.Fatal(err)
5757
return
5858
}
@@ -127,6 +127,22 @@ func validateLabels(ls []string) error {
127127
return nil
128128
}
129129

130+
func (r *registerInputs) stageValue(stage registerStage) string {
131+
switch stage {
132+
case StageInputInstance:
133+
return r.InstanceAddr
134+
case StageInputToken:
135+
return r.Token
136+
case StageInputRunnerName:
137+
return r.RunnerName
138+
case StageInputLabels:
139+
if len(r.Labels) > 0 {
140+
return strings.Join(r.Labels, ",")
141+
}
142+
}
143+
return ""
144+
}
145+
130146
func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *config.Config) registerStage {
131147
// must set instance address and token.
132148
// if empty, keep current stage.
@@ -181,18 +197,33 @@ func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *co
181197

182198
if validateLabels(r.Labels) != nil {
183199
log.Infoln("Invalid labels, please input again, leave blank to use the default labels (for example, ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest)")
200+
r.Labels = nil
184201
return StageInputLabels
185202
}
186203
return StageWaitingForRegistration
187204
}
188205
return StageUnknown
189206
}
190207

191-
func registerInteractive(ctx context.Context, configFile string) error {
208+
func initInputs(regArgs *registerArgs) *registerInputs {
209+
inputs := &registerInputs{
210+
InstanceAddr: regArgs.InstanceAddr,
211+
Token: regArgs.Token,
212+
RunnerName: regArgs.RunnerName,
213+
Ephemeral: regArgs.Ephemeral,
214+
}
215+
regArgs.Labels = strings.TrimSpace(regArgs.Labels)
216+
// command line flag.
217+
if regArgs.Labels != "" {
218+
inputs.Labels = strings.Split(regArgs.Labels, ",")
219+
}
220+
return inputs
221+
}
222+
223+
func registerInteractive(ctx context.Context, configFile string, regArgs *registerArgs) error {
192224
var (
193225
reader = bufio.NewReader(os.Stdin)
194226
stage = StageInputInstance
195-
inputs = new(registerInputs)
196227
)
197228

198229
cfg, err := config.LoadDefault(configFile)
@@ -202,13 +233,17 @@ func registerInteractive(ctx context.Context, configFile string) error {
202233
if f, err := os.Stat(cfg.Runner.File); err == nil && !f.IsDir() {
203234
stage = StageOverwriteLocalConfig
204235
}
236+
inputs := initInputs(regArgs)
205237

206238
for {
207-
printStageHelp(stage)
208-
209-
cmdString, err := reader.ReadString('\n')
210-
if err != nil {
211-
return err
239+
cmdString := inputs.stageValue(stage)
240+
if cmdString == "" {
241+
printStageHelp(stage)
242+
var err error
243+
cmdString, err = reader.ReadString('\n')
244+
if err != nil {
245+
return err
246+
}
212247
}
213248
stage = inputs.assignToNext(stage, strings.TrimSpace(cmdString), cfg)
214249

@@ -255,25 +290,17 @@ func registerNoInteractive(ctx context.Context, configFile string, regArgs *regi
255290
if err != nil {
256291
return err
257292
}
258-
inputs := &registerInputs{
259-
InstanceAddr: regArgs.InstanceAddr,
260-
Token: regArgs.Token,
261-
RunnerName: regArgs.RunnerName,
262-
Labels: defaultLabels,
263-
Ephemeral: regArgs.Ephemeral,
264-
}
265-
regArgs.Labels = strings.TrimSpace(regArgs.Labels)
266-
// command line flag.
267-
if regArgs.Labels != "" {
268-
inputs.Labels = strings.Split(regArgs.Labels, ",")
269-
}
293+
inputs := initInputs(regArgs)
270294
// specify labels in config file.
271295
if len(cfg.Runner.Labels) > 0 {
272296
if regArgs.Labels != "" {
273297
log.Warn("Labels from command will be ignored, use labels defined in config file.")
274298
}
275299
inputs.Labels = cfg.Runner.Labels
276300
}
301+
if len(inputs.Labels) == 0 {
302+
inputs.Labels = defaultLabels
303+
}
277304

278305
if inputs.RunnerName == "" {
279306
inputs.RunnerName, _ = os.Hostname()

0 commit comments

Comments
 (0)