feat: parse register arguments and do register when non-interactive mode
This commit is contained in:
parent
ab4e06f977
commit
45de6199d1
2 changed files with 59 additions and 13 deletions
|
@ -45,9 +45,11 @@ func Execute(ctx context.Context) {
|
||||||
Args: cobra.MaximumNArgs(0),
|
Args: cobra.MaximumNArgs(0),
|
||||||
RunE: runRegister(ctx, ®Args, gArgs.EnvFile), // must use a pointer to regArgs
|
RunE: runRegister(ctx, ®Args, gArgs.EnvFile), // must use a pointer to regArgs
|
||||||
}
|
}
|
||||||
registerCmd.Flags().BoolVarP(®Args.NoInteractive, "no-interactive", "", false, "Disable interactive mode")
|
registerCmd.Flags().BoolVar(®Args.NoInteractive, "no-interactive", false, "Disable interactive mode")
|
||||||
registerCmd.Flags().StringVarP(®Args.InstanceAddr, "instance", "", "", "Gitea instance address")
|
registerCmd.Flags().StringVar(®Args.InstanceAddr, "instance", "", "Gitea instance address")
|
||||||
registerCmd.Flags().StringVarP(®Args.Token, "token", "", "", "Runner token")
|
registerCmd.Flags().StringVar(®Args.Token, "token", "", "Runner token")
|
||||||
|
registerCmd.Flags().StringVar(®Args.RunnerName, "name", "", "Runner name")
|
||||||
|
registerCmd.Flags().StringVar(®Args.Labels, "labels", "", "Runner tags, comma separated")
|
||||||
rootCmd.AddCommand(registerCmd)
|
rootCmd.AddCommand(registerCmd)
|
||||||
|
|
||||||
// ./act_runner daemon
|
// ./act_runner daemon
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -41,6 +42,11 @@ func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) fun
|
||||||
log.Warnf("Runner in user-mode.")
|
log.Warnf("Runner in user-mode.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if regArgs.NoInteractive {
|
||||||
|
if err := registerNoInteractive(envFile, regArgs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
go func() {
|
go func() {
|
||||||
if err := registerInteractive(envFile); err != nil {
|
if err := registerInteractive(envFile); err != nil {
|
||||||
// log.Errorln(err)
|
// log.Errorln(err)
|
||||||
|
@ -53,6 +59,7 @@ func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) fun
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt)
|
signal.Notify(c, os.Interrupt)
|
||||||
<-c
|
<-c
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -63,6 +70,8 @@ type registerArgs struct {
|
||||||
NoInteractive bool
|
NoInteractive bool
|
||||||
InstanceAddr string
|
InstanceAddr string
|
||||||
Token string
|
Token string
|
||||||
|
RunnerName string
|
||||||
|
Labels string
|
||||||
}
|
}
|
||||||
|
|
||||||
type registerStage int8
|
type registerStage int8
|
||||||
|
@ -85,6 +94,16 @@ type registerInputs struct {
|
||||||
CustomLabels []string
|
CustomLabels []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *registerInputs) validate() error {
|
||||||
|
if r.InstanceAddr == "" {
|
||||||
|
return fmt.Errorf("instance address is empty")
|
||||||
|
}
|
||||||
|
if r.Token == "" {
|
||||||
|
return fmt.Errorf("token is empty")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *registerInputs) assignToNext(stage registerStage, value string) registerStage {
|
func (r *registerInputs) assignToNext(stage registerStage, value string) registerStage {
|
||||||
|
|
||||||
// must set instance address and token.
|
// must set instance address and token.
|
||||||
|
@ -191,6 +210,31 @@ func printStageHelp(stage registerStage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerNoInteractive(envFile string, regArgs *registerArgs) error {
|
||||||
|
_ = godotenv.Load(envFile)
|
||||||
|
cfg, _ := config.FromEnviron()
|
||||||
|
inputs := ®isterInputs{
|
||||||
|
InstanceAddr: regArgs.InstanceAddr,
|
||||||
|
Token: regArgs.Token,
|
||||||
|
RunnerName: regArgs.RunnerName,
|
||||||
|
CustomLabels: strings.Split(regArgs.Labels, ","),
|
||||||
|
}
|
||||||
|
if inputs.RunnerName == "" {
|
||||||
|
inputs.RunnerName, _ = os.Hostname()
|
||||||
|
log.Infof("Runner name is empty, use hostname '%s'.", inputs.RunnerName)
|
||||||
|
}
|
||||||
|
if err := inputs.validate(); err != nil {
|
||||||
|
log.WithError(err).Errorf("Invalid input, please re-run act command.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := doRegister(&cfg, inputs); err != nil {
|
||||||
|
log.Errorf("Failed to register runner: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Infof("Runner registered successfully.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func doRegister(cfg *config.Config, inputs *registerInputs) error {
|
func doRegister(cfg *config.Config, inputs *registerInputs) error {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
Loading…
Reference in a new issue