2024-03-13 10:08:22 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2024-03-13 10:08:22 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2024-03-13 10:08:22 +00:00
|
|
|
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/internal/config"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/setup/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the setup command.
|
|
|
|
var Command = &cli.Command{
|
2024-03-28 09:36:39 +00:00
|
|
|
Name: "setup",
|
|
|
|
Usage: "setup the woodpecker-cli for the first time",
|
2024-06-06 11:30:08 +00:00
|
|
|
ArgsUsage: "[server]",
|
2024-03-13 10:08:22 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2024-06-06 11:30:08 +00:00
|
|
|
Name: "server",
|
2024-11-30 14:23:44 +00:00
|
|
|
Usage: "URL of the woodpecker server",
|
2024-03-13 10:08:22 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "token",
|
2024-11-30 14:23:44 +00:00
|
|
|
Usage: "token to authenticate with the woodpecker server",
|
2024-03-13 10:08:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: setup,
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
func setup(ctx context.Context, c *cli.Command) error {
|
|
|
|
_config, err := config.Get(ctx, c, c.String("config"))
|
2024-03-13 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if _config != nil {
|
|
|
|
setupAgain, err := ui.Confirm("The woodpecker-cli was already configured. Do you want to configure it again?")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !setupAgain {
|
2024-11-30 14:23:44 +00:00
|
|
|
log.Info().Msg("configuration skipped")
|
2024-03-13 10:08:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:30:08 +00:00
|
|
|
serverURL := c.String("server")
|
2024-03-28 09:36:39 +00:00
|
|
|
if serverURL == "" {
|
|
|
|
serverURL = c.Args().First()
|
|
|
|
}
|
2024-03-13 10:08:22 +00:00
|
|
|
|
|
|
|
if serverURL == "" {
|
|
|
|
serverURL, err = ui.Ask("Enter the URL of the woodpecker server", "https://ci.woodpecker-ci.org", true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverURL == "" {
|
|
|
|
return errors.New("server URL cannot be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(serverURL, "://") {
|
|
|
|
serverURL = "https://" + serverURL
|
|
|
|
}
|
|
|
|
|
|
|
|
token := c.String("token")
|
|
|
|
if token == "" {
|
2024-07-17 23:26:35 +00:00
|
|
|
token, err = receiveTokenFromUI(ctx, serverURL)
|
2024-03-13 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
return errors.New("no token received from the UI")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
err = config.Save(ctx, c, c.String("config"), &config.Config{
|
2024-03-13 10:08:22 +00:00
|
|
|
ServerURL: serverURL,
|
|
|
|
Token: token,
|
|
|
|
LogLevel: "info",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-30 14:23:44 +00:00
|
|
|
log.Info().Msg("woodpecker-cli has been successfully setup")
|
2024-03-13 10:08:22 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|