// Copyright 2025 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package base import ( "fmt" "log" "os" "code.gitea.io/tea/modules/config" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/utils" gogit "github.com/go-git/go-git/v5" "github.com/urfave/cli/v3" ) // InitCommand resolves the application context, and returns the active login, and if // available the repo slug. It does this by reading the config file for logins, parsing // the remotes of the .git repo specified in repoFlag or $PWD, and using overrides from // command flags. If a local git repo can't be found, repo slug values are unset. func InitCommand(cmd *cli.Command) *context.TeaContext { // these flags are used as overrides to the context detection via local git repo repoFlag := cmd.String("repo") loginFlag := cmd.String("login") remoteFlag := cmd.String("remote") var ( c context.TeaContext err error repoPath string // empty means PWD repoFlagPathExists bool ) // check if repoFlag can be interpreted as path to local repo. if len(repoFlag) != 0 { if repoFlagPathExists, err = utils.DirExists(repoFlag); err != nil { log.Fatal(err.Error()) } if repoFlagPathExists { repoPath = repoFlag } } if len(remoteFlag) == 0 { remoteFlag = config.GetPreferences().FlagDefaults.Remote } if repoPath == "" { if repoPath, err = os.Getwd(); err != nil { log.Fatal(err.Error()) } } // try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir, // otherwise attempt PWD. if no repo is found, continue with default login if c.LocalRepo, c.Login, c.RepoSlug, err = context.ContextFromLocalRepo(repoPath, remoteFlag); err != nil { if err == context.ErrNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { // we can deal with that, commands needing the optional values use ctx.Ensure() } else { log.Fatal(err.Error()) } } if len(repoFlag) != 0 && !repoFlagPathExists { // if repoFlag is not a valid path, use it to override repoSlug c.RepoSlug = repoFlag } // override config user with env variable envLogin := context.GetLoginByEnvVar() if envLogin != nil { _, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "") if err != nil { log.Fatal(err.Error()) } c.Login = envLogin } // override login from flag, or use default login if repo based detection failed if len(loginFlag) != 0 { c.Login = config.GetLoginByName(loginFlag) if c.Login == nil { log.Fatalf("Login name '%s' does not exist", loginFlag) } } else if c.Login == nil { c.Login, err = interact.LoginSelect() if err != nil { // TODO: maybe we can directly start interact.CreateLogin() (only if // we're sure we can interactively!), as gh cli does. fmt.Println(`No gitea login configured. To start using tea, first run tea login add and then run your command again.`) os.Exit(1) } } // parse reposlug (owner falling back to login owner if reposlug contains only repo name) c.Owner, c.Repo = utils.GetOwnerAndRepo(c.RepoSlug, c.Login.User) c.Command = cmd c.Output = cmd.String("output") return &c }