Esempio n. 1
0
File: db.go Progetto: arteev/dsql
func deleteDatabase() cli.Command {
	return cli.Command{
		Name:  "delete",
		Usage: "Delete database by code",
		Flags: []cli.Flag{
			stringFlag("code", ""),
		},
		Action: func(ctx *cli.Context) {
			logger.Trace.Println("command database delete")
			if !ctx.IsSet("code") {
				panic(fmt.Errorf("option code must be set"))
			}
			code := ctx.String("code")
			logger.Debug.Printf("database deleting %q\n", code)
			d := db.GetInstance()
			dbfind, err := d.FindByCode(code)
			if err != nil {
				panic(err)
			}
			if err := d.Delete(dbfind); err != nil {
				panic(err)
			}
			logger.Info.Printf("database %q deleted\n", code)
		},
	}
}
Esempio n. 2
0
File: db.go Progetto: arteev/dsql
func updateDatabase() cli.Command {
	return cli.Command{
		Name:  "update",
		Usage: "Update database",
		Flags: []cli.Flag{
			stringFlag("code", ""),
			stringFlag("newcode", ""),
			stringFlag("uri", ""),
			stringFlag("engine", ""),
			cli.BoolFlag{
				Name:  "enabled",
				Usage: "enabled or disable database",
			},
		},
		Action: func(ctx *cli.Context) {
			logger.Trace.Println("command db update")
			if !ctx.IsSet("code") {
				panic(fmt.Errorf("option code must be set"))
			}
			code := ctx.String("code")
			logger.Debug.Printf("updating %s, new values(code:%s; uri:%s; enabled:%v; engine:%v)\n", code, ctx.String("code"), ctx.String("uri"), ctx.Bool("enabled"), ctx.String("engine"))
			d := db.GetInstance()
			dbFind, err := d.FindByCode(code)
			logger.Debug.Println(dbFind)
			if err != nil {
				panic(err)
			}
			if ctx.IsSet("newcode") {
				dbFind.Code = ctx.String("newcode")
			}
			if ctx.IsSet("uri") {
				dbFind.ConnectionString = ctx.String("uri")
			}
			if ctx.IsSet("enabled") {
				dbFind.Enabled = ctx.Bool("enabled")
			}

			if ctx.IsSet("engine") {
				dbFind.Engine = ctx.String("engine")
				rdb.CheckCodeEngine(dbFind.Engine)
			}
			if err := d.Update(dbFind); err != nil {
				panic(err)
			}

			logger.Info.Println("updated ", code)

		},
	}

}
Esempio n. 3
0
//commonActionDBS. Returns action for cli app bind with handler for current db item
func commonActionDBS(cflags *cliFlags, name string, a action.Actioner, sqlRequired bool, before, after, errtrg actionTriggerDBS) func(ctx *cli.Context) {
	return func(ctx *cli.Context) {
		logger.Trace.Println("command", name)
		defer logger.Trace.Println("command", name, "done")
		d := db.GetInstance()
		defer checkErr(d.Close)
		cflags.SetContext(ctx)
		dbsSource, err := getDatabases(cflags, d)
		if err != nil {
			panic(err)
		}
		if len(dbsSource) == 0 {
			panic(fmt.Errorf("databases not found"))
		}

		paramGetter := createParametersGetter(ctx)

		var sc *sqlcommand.SQLCommand
		if sqlRequired {
			sc = parseSQLFromArgs(ctx)
			if sc == nil {
				panic(fmt.Errorf("SQL not found"))
			}
			logger.Debug.Printf("select sql:%q, %v\n", sc.Script, sc.Params)
		}

		contextRun := action.NewContext()

		if err := parseOthersFlagsForRunContext(ctx, contextRun); err != nil {
			panic(err)
		}

		doTrigger(before, dbsSource, contextRun)
		if _, e := run.Run(dbsSource, sc, a, contextRun, paramGetter); e != nil {
			logger.Error.Println(e)
			doTrigger(errtrg, dbsSource, contextRun)
		} else {

			if success, ok := contextRun.Get("success").(int); !ok || success == 0 {
				doTrigger(errtrg, dbsSource, contextRun)
			} else {
				doTrigger(after, dbsSource, contextRun)
			}
		}

	}
}
Esempio n. 4
0
File: db.go Progetto: arteev/dsql
func addDatabase() cli.Command {
	return cli.Command{
		Name:  "add",
		Usage: "Add new database",
		Flags: []cli.Flag{
			stringFlag("code", ""),
			stringFlag("uri", ""),
			stringFlag("engine", ""),
		},
		Action: func(ctx *cli.Context) {
			logger.Trace.Println("command db add")
			for _, flag := range ctx.FlagNames() {
				if !ctx.IsSet(flag) {
					panic(fmt.Errorf("option %q must be set", flag))
				}
			}
			d := db.GetInstance()
			engine := ctx.String("engine")
			rdb.CheckCodeEngine(engine)
			newdb := db.Database{
				Code:             ctx.String("code"),
				ConnectionString: ctx.String("uri"),
				Enabled:          true,
				Engine:           engine,
			}
			logger.Debug.Println("Adding ", newdb.Code, newdb.ConnectionString)
			err := d.Add(newdb)
			if err != nil {
				panic(err)
			}
			logger.Info.Println("Added ", newdb.Code)

		},
	}

}
Esempio n. 5
0
File: db.go Progetto: arteev/dsql
func listDatabase() cli.Command {

	dbFilterFlags := newCliFlags(cliOption{
		Databases:        modeFlagMulti,
		ExcludeDatabases: modeFlagMulti,
		Engines:          modeFlagMulti,
		Tags:             modeFlagMulti,
		ExcludeTags:      modeFlagMulti,
	})

	return cli.Command{
		Name:  "list",
		Usage: "list of databases",
		Flags: append(dbFilterFlags.Flags(),
			cli.BoolFlag{
				Name:  "fit",
				Usage: "use for fit table by width window of terminal",
			},
			cli.StringFlag{
				Name:  "border",
				Usage: "set type of border table: Thin,Double,Simple or None. Default:Thin",
			}),
		Action: func(ctx *cli.Context) error {
			logger.Trace.Println("command list database")
			dbFilterFlags.SetContext(ctx)
			d := db.GetInstance()
			dbs, err := d.All()
			if err != nil {
				return err
			}
			for _, e := range dbFilterFlags.Engines() {
				rdb.CheckCodeEngine(e)
			}
			dbFilterFlags.ApplyTo(dbs)

			tab := fmttab.New("List of databases", fmttab.BorderThin, nil)
			tab.AddColumn("Id", 4, fmttab.AlignRight)
			tab.AddColumn("On", 2, fmttab.AlignLeft)
			tab.AddColumn("Code", 10, fmttab.AlignLeft)
			tab.AddColumn("Engine", 11, fmttab.AlignLeft)
			tab.AddColumn("URI", 40, fmttab.AlignLeft)
			tab.AddColumn("Tags", 25, fmttab.AlignLeft)
			for _, curd := range dbs.Get() {
				rec := make(map[string]interface{})
				rec["Id"] = curd.ID
				if curd.Enabled {
					rec["On"] = "+"
				}
				rec["Code"] = curd.Code
				rec["URI"] = curd.ConnectionString
				rec["Engine"] = curd.Engine
				rec["Tags"] = curd.TagsComma(";")
				tab.AppendData(rec)
			}
			pget := parametergetter.New(ctx, parameters.GetInstance())
			if pget.GetDef(parametergetter.Fit, false).(bool) {
				if e := termbox.Init(); e != nil {
					panic(e)
				}
				tw, _ := termbox.Size()
				tab.AutoSize(true, tw)
				termbox.Close()
			}
			switch pget.GetDef(parametergetter.BorderTable, "").(string) {
			case "Thin":
				tab.SetBorder(fmttab.BorderThin)
			case "Double":
				tab.SetBorder(fmttab.BorderDouble)
			case "None":
				tab.SetBorder(fmttab.BorderNone)
			case "Simple":
				tab.SetBorder(fmttab.BorderSimple)
			}
			_, err = tab.WriteTo(os.Stdout)
			return err
		},
	}
}
Esempio n. 6
0
File: db.go Progetto: arteev/dsql
func tagDatabase() cli.Command {
	dbFilterFlags := newCliFlags(cliOption{
		Databases:        modeFlagMulti,
		ExcludeDatabases: modeFlagMulti,
		Engines:          modeFlagMulti,
		Tags:             modeFlagUnUsed,
		ExcludeTags:      modeFlagUnUsed,
	})

	flags := dbFilterFlags.Flags()
	flags = append(flags, cli.StringSliceFlag{
		Name:  "add",
		Usage: "new tag(s)",
	})
	flags = append(flags, cli.StringSliceFlag{
		Name:  "remove",
		Usage: "remove tag(s)",
	})

	return cli.Command{
		Name:  "tag",
		Usage: "add or remove tag for database",
		Flags: flags,
		Action: func(ctx *cli.Context) {
			logger.Trace.Println("command db tag")
			defer logger.Trace.Println("command db tag done")

			var add, remove = ctx.StringSlice("add"), ctx.StringSlice("remove")
			if len(add) == 0 && len(remove) == 0 {
				panic(fmt.Errorf("must be set: new tag or del tag"))
			}
			dbFilterFlags.SetContext(ctx)

			logger.Debug.Printf("updating new:%s remove:%s\n", add, remove)

			d := db.GetInstance()
			col, err := d.All()
			if err != nil {
				panic(err)
			}
			for _, e := range dbFilterFlags.Engines() {
				rdb.CheckCodeEngine(e)
			}
			dbFilterFlags.ApplyTo(col)

			dbs := col.Get()
			if len(dbs) == 0 {
				panic("databases not found")
			}

			pget := parametergetter.New(ctx, parameters.GetInstance())
			showstat := pget.GetDef(parametergetter.Statistic, false).(bool)
			var cntadd, cntremove int

			for _, curdb := range dbs {
				logger.Trace.Printf("process tag: %q\n", curdb.Code)

				if cnt, err := d.AddTags(&curdb, add...); err != nil {
					panic(err)
				} else {
					cntadd += cnt
					logger.Info.Printf("Added tags %d for %s\n", cnt, curdb.Code)
				}

				if cnt, err := d.RemoveTags(&curdb, remove...); err != nil {
					panic(err)
				} else {
					cntremove += cnt
					logger.Info.Printf("Removed tags %d for %s\n", cnt, curdb.Code)
				}
			}
			if showstat {
				fmt.Printf("Added tags: %d\nRemoved tags: %d\n", cntadd, cntremove)
			}

		},
	}
}