mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-20 11:29:13 -05:00
/go/{cmd,libraries}: add ci ls too
This commit is contained in:
@@ -23,4 +23,5 @@ var Commands = cli.NewHiddenSubCommandHandler("ci", "Commands for working with D
|
||||
DestroyCmd{},
|
||||
ImportCmd{},
|
||||
ExportCmd{},
|
||||
ListCmd{},
|
||||
})
|
||||
|
||||
@@ -91,7 +91,7 @@ func (cmd ExportCmd) Exec(ctx context.Context, commandStr string, args []string,
|
||||
|
||||
workflowName := apr.Arg(0)
|
||||
|
||||
querist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
|
||||
queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
@@ -113,14 +113,14 @@ func (cmd ExportCmd) Exec(ctx context.Context, commandStr string, args []string,
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(fmt.Errorf("dolt ci has not been initialized, please initialize with: dolt ci init")), usage)
|
||||
}
|
||||
|
||||
wr := dolt_ci.NewWorkflowManager(user, email, querist.Query)
|
||||
wm := dolt_ci.NewWorkflowManager(user, email, queryist.Query)
|
||||
|
||||
db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
config, err := wr.GetWorkflowConfig(sqlCtx, db, workflowName)
|
||||
config, err := wm.GetWorkflowConfig(sqlCtx, db, workflowName)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ func (cmd ImportCmd) Exec(ctx context.Context, commandStr string, args []string,
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
querist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
|
||||
queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
@@ -126,14 +126,14 @@ func (cmd ImportCmd) Exec(ctx context.Context, commandStr string, args []string,
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
wr := dolt_ci.NewWorkflowManager(user, email, querist.Query)
|
||||
wm := dolt_ci.NewWorkflowManager(user, email, queryist.Query)
|
||||
|
||||
db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
err = wr.StoreAndCommit(sqlCtx, db, workflowConfig)
|
||||
err = wm.StoreAndCommit(sqlCtx, db, workflowConfig)
|
||||
if err != nil {
|
||||
errorText := err.Error()
|
||||
switch {
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
// Copyright 2024 Dolthub, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ci
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/cli"
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/commands"
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/env"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions/dolt_ci"
|
||||
"github.com/dolthub/dolt/go/libraries/utils/argparser"
|
||||
)
|
||||
|
||||
var listDocs = cli.CommandDocumentationContent{
|
||||
ShortDesc: "List Dolt CI workflows",
|
||||
LongDesc: "List Dolt CI workflows",
|
||||
Synopsis: []string{
|
||||
"{{.LessThan}}ls{{.GreaterThan}}",
|
||||
},
|
||||
}
|
||||
|
||||
type ListCmd struct{}
|
||||
|
||||
// Name implements cli.Command.
|
||||
func (cmd ListCmd) Name() string {
|
||||
return "ls"
|
||||
}
|
||||
|
||||
// Description implements cli.Command.
|
||||
func (cmd ListCmd) Description() string {
|
||||
return listDocs.ShortDesc
|
||||
}
|
||||
|
||||
// RequiresRepo implements cli.Command.
|
||||
func (cmd ListCmd) RequiresRepo() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Docs implements cli.Command.
|
||||
func (cmd ListCmd) Docs() *cli.CommandDocumentation {
|
||||
ap := cmd.ArgParser()
|
||||
return cli.NewCommandDocumentation(listDocs, ap)
|
||||
}
|
||||
|
||||
// Hidden should return true if this command should be hidden from the help text
|
||||
func (cmd ListCmd) Hidden() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ArgParser implements cli.Command.
|
||||
func (cmd ListCmd) ArgParser() *argparser.ArgParser {
|
||||
ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 0)
|
||||
return ap
|
||||
}
|
||||
|
||||
// Exec implements cli.Command.
|
||||
func (cmd ListCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int {
|
||||
ap := cmd.ArgParser()
|
||||
_, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, listDocs, ap))
|
||||
|
||||
if !cli.CheckEnvIsValid(dEnv) {
|
||||
return 1
|
||||
}
|
||||
|
||||
queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
if closeFunc != nil {
|
||||
defer closeFunc()
|
||||
}
|
||||
|
||||
db, err := newDatabase(sqlCtx, sqlCtx.GetCurrentDatabase(), dEnv, false)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
name, email, err := env.GetNameAndEmail(dEnv.Config)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
hasTables, err := dolt_ci.HasDoltCITables(sqlCtx)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
if !hasTables {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(fmt.Errorf("dolt ci has not been initialized, please initialize with: dolt ci init")), usage)
|
||||
}
|
||||
|
||||
wm := dolt_ci.NewWorkflowManager(name, email, queryist.Query)
|
||||
|
||||
workflows, err := wm.ListWorkflows(sqlCtx, db)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
for _, w := range workflows {
|
||||
cli.Println(color.CyanString(fmt.Sprintf("%s", w)))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -40,6 +40,7 @@ var ErrWorkflowNotFound = errors.New("workflow not found")
|
||||
var ErrMultipleWorkflowsFound = errors.New("multiple workflows found")
|
||||
|
||||
type WorkflowManager interface {
|
||||
ListWorkflows(ctx *sql.Context, db sqle.Database) ([]string, error)
|
||||
GetWorkflowConfig(ctx *sql.Context, db sqle.Database, workflowName string) (*WorkflowConfig, error)
|
||||
StoreAndCommit(ctx *sql.Context, db sqle.Database, config *WorkflowConfig) error
|
||||
}
|
||||
@@ -700,6 +701,11 @@ func (d *doltWorkflowManager) listWorkflowEventsByWorkflowName(ctx *sql.Context,
|
||||
return d.retrieveWorkflowEvents(ctx, query)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) listWorkflows(ctx *sql.Context) ([]*Workflow, error) {
|
||||
query := d.selectAllFromWorkflowsTableQuery()
|
||||
return d.retrieveWorkflows(ctx, query)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) listWorkflowEventsByWorkflowNameWhereEventTypeIsPush(ctx *sql.Context, workflowName WorkflowName) ([]*WorkflowEvent, error) {
|
||||
query := d.selectAllFromWorkflowEventsTableByWorkflowNameWhereEventTypeIsPushQuery(string(workflowName))
|
||||
return d.retrieveWorkflowEvents(ctx, query)
|
||||
@@ -1940,6 +1946,24 @@ func (d *doltWorkflowManager) GetWorkflowConfig(ctx *sql.Context, db sqle.Databa
|
||||
return d.getWorkflowConfig(ctx, workflowName)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) ListWorkflows(ctx *sql.Context, db sqle.Database) ([]string, error) {
|
||||
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Read); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, 0)
|
||||
workflows, err := d.listWorkflows(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(workflows, func(i, j int) bool {
|
||||
return *workflows[i].Name < *workflows[j].Name
|
||||
})
|
||||
for _, w := range workflows {
|
||||
names = append(names, string(*w.Name))
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) StoreAndCommit(ctx *sql.Context, db sqle.Database, config *WorkflowConfig) error {
|
||||
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user