|
| 1 | +// Copyright 2024 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ci |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/dolthub/dolt/go/libraries/doltcore/env/actions/dolt_ci" |
| 22 | + |
| 23 | + "github.com/dolthub/dolt/go/cmd/dolt/cli" |
| 24 | + "github.com/dolthub/dolt/go/cmd/dolt/commands" |
| 25 | + "github.com/dolthub/dolt/go/cmd/dolt/errhand" |
| 26 | + "github.com/dolthub/dolt/go/libraries/doltcore/env" |
| 27 | + "github.com/dolthub/dolt/go/libraries/doltcore/sqle" |
| 28 | + "github.com/dolthub/dolt/go/libraries/doltcore/table/editor" |
| 29 | + "github.com/dolthub/dolt/go/libraries/utils/argparser" |
| 30 | +) |
| 31 | + |
| 32 | +var initDocs = cli.CommandDocumentationContent{ |
| 33 | + ShortDesc: "Creates database tables used to store continuous integration configuration", |
| 34 | + LongDesc: "Creates database tables used to store continuous integration configuration", |
| 35 | + Synopsis: []string{ |
| 36 | + "{{.LessThan}}init{{.GreaterThan}}", |
| 37 | + }, |
| 38 | +} |
| 39 | + |
| 40 | +type InitCmd struct{} |
| 41 | + |
| 42 | +// Name implements cli.Command. |
| 43 | +func (cmd InitCmd) Name() string { |
| 44 | + return "init" |
| 45 | +} |
| 46 | + |
| 47 | +// Description implements cli.Command. |
| 48 | +func (cmd InitCmd) Description() string { |
| 49 | + return initDocs.ShortDesc |
| 50 | +} |
| 51 | + |
| 52 | +// RequiresRepo implements cli.Command. |
| 53 | +func (cmd InitCmd) RequiresRepo() bool { |
| 54 | + return true |
| 55 | +} |
| 56 | + |
| 57 | +// Docs implements cli.Command. |
| 58 | +func (cmd InitCmd) Docs() *cli.CommandDocumentation { |
| 59 | + ap := cmd.ArgParser() |
| 60 | + return cli.NewCommandDocumentation(initDocs, ap) |
| 61 | +} |
| 62 | + |
| 63 | +// Hidden should return true if this command should be hidden from the help text |
| 64 | +func (cmd InitCmd) Hidden() bool { |
| 65 | + return true |
| 66 | +} |
| 67 | + |
| 68 | +// ArgParser implements cli.Command. |
| 69 | +func (cmd InitCmd) ArgParser() *argparser.ArgParser { |
| 70 | + ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 0) |
| 71 | + return ap |
| 72 | +} |
| 73 | + |
| 74 | +// Exec implements cli.Command. |
| 75 | +func (cmd InitCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int { |
| 76 | + ap := cmd.ArgParser() |
| 77 | + _, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, initDocs, ap)) |
| 78 | + |
| 79 | + if !cli.CheckEnvIsValid(dEnv) { |
| 80 | + return 1 |
| 81 | + } |
| 82 | + |
| 83 | + _, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx) |
| 84 | + if err != nil { |
| 85 | + return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage) |
| 86 | + } |
| 87 | + if closeFunc != nil { |
| 88 | + defer closeFunc() |
| 89 | + } |
| 90 | + |
| 91 | + db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false) |
| 92 | + if err != nil { |
| 93 | + return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage) |
| 94 | + } |
| 95 | + |
| 96 | + name, email, err := env.GetNameAndEmail(dEnv.Config) |
| 97 | + if err != nil { |
| 98 | + return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage) |
| 99 | + } |
| 100 | + |
| 101 | + hasTables, err := dolt_ci.HasDoltCITables(sqlCtx) |
| 102 | + if err != nil { |
| 103 | + return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage) |
| 104 | + } |
| 105 | + |
| 106 | + if hasTables { |
| 107 | + return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(fmt.Errorf("dolt ci has already been initialized")), usage) |
| 108 | + } |
| 109 | + |
| 110 | + var verr errhand.VerboseError |
| 111 | + err = dolt_ci.CreateDoltCITables(sqlCtx, db, name, email) |
| 112 | + if err != nil { |
| 113 | + verr = errhand.VerboseErrorFromError(err) |
| 114 | + } |
| 115 | + |
| 116 | + return commands.HandleVErrAndExitCode(verr, usage) |
| 117 | +} |
| 118 | + |
| 119 | +func newDatabase(ctx context.Context, name string, dEnv *env.DoltEnv, useBulkEditor bool) (sqle.Database, error) { |
| 120 | + deaf := dEnv.DbEaFactory() |
| 121 | + if useBulkEditor { |
| 122 | + deaf = dEnv.BulkDbEaFactory() |
| 123 | + } |
| 124 | + tmpDir, err := dEnv.TempTableFilesDir() |
| 125 | + if err != nil { |
| 126 | + return sqle.Database{}, err |
| 127 | + } |
| 128 | + opts := editor.Options{ |
| 129 | + Deaf: deaf, |
| 130 | + Tempdir: tmpDir, |
| 131 | + } |
| 132 | + return sqle.NewDatabase(ctx, name, dEnv.DbData(), opts) |
| 133 | +} |
0 commit comments