1// Copyright 2018 The Gitea Authors. All rights reserved. 2// Use of this source code is governed by a MIT-style 3// license that can be found in the LICENSE file. 4 5// Package cmd provides subcommands to the gitea binary - such as "web" or 6// "admin". 7package cmd 8 9import ( 10 "context" 11 "errors" 12 "fmt" 13 "os" 14 "os/signal" 15 "strings" 16 "syscall" 17 18 "code.gitea.io/gitea/models/db" 19 "code.gitea.io/gitea/modules/log" 20 "code.gitea.io/gitea/modules/setting" 21 "code.gitea.io/gitea/modules/util" 22 23 "github.com/urfave/cli" 24) 25 26// argsSet checks that all the required arguments are set. args is a list of 27// arguments that must be set in the passed Context. 28func argsSet(c *cli.Context, args ...string) error { 29 for _, a := range args { 30 if !c.IsSet(a) { 31 return errors.New(a + " is not set") 32 } 33 34 if util.IsEmptyString(c.String(a)) { 35 return errors.New(a + " is required") 36 } 37 } 38 return nil 39} 40 41// confirm waits for user input which confirms an action 42func confirm() (bool, error) { 43 var response string 44 45 _, err := fmt.Scanln(&response) 46 if err != nil { 47 return false, err 48 } 49 50 switch strings.ToLower(response) { 51 case "y", "yes": 52 return true, nil 53 case "n", "no": 54 return false, nil 55 default: 56 return false, errors.New(response + " isn't a correct confirmation string") 57 } 58} 59 60func initDB(ctx context.Context) error { 61 setting.LoadFromExisting() 62 setting.InitDBConfig() 63 setting.NewXORMLogService(false) 64 65 if setting.Database.Type == "" { 66 log.Fatal(`Database settings are missing from the configuration file: %q. 67Ensure you are running in the correct environment or set the correct configuration file with -c. 68If this is the intended configuration file complete the [database] section.`, setting.CustomConf) 69 } 70 if err := db.InitEngine(ctx); err != nil { 71 return fmt.Errorf("unable to initialise the database using the configuration in %q. Error: %v", setting.CustomConf, err) 72 } 73 return nil 74} 75 76func installSignals() (context.Context, context.CancelFunc) { 77 ctx, cancel := context.WithCancel(context.Background()) 78 go func() { 79 // install notify 80 signalChannel := make(chan os.Signal, 1) 81 82 signal.Notify( 83 signalChannel, 84 syscall.SIGINT, 85 syscall.SIGTERM, 86 ) 87 select { 88 case <-signalChannel: 89 case <-ctx.Done(): 90 } 91 cancel() 92 signal.Reset() 93 }() 94 95 return ctx, cancel 96} 97