Exemplo n.º 1
0
//getDatabases return get enabled database with filter by name from args application
func getDatabases(cflags *cliFlags, r db.RepositoryDB) ([]db.Database, error) {
	dbs, err := r.All()
	if err != nil {
		return nil, err
	}
	dbs.AddFilterEnabled()
	cflags.ApplyTo(dbs)
	for _, e := range cflags.Engines() {
		rdb.CheckCodeEngine(e)
	}
	result := dbs.Get()
	return result, nil
}
Exemplo n.º 2
0
Arquivo: db.go Projeto: 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)

		},
	}

}
Exemplo n.º 3
0
Arquivo: db.go Projeto: 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)

		},
	}

}
Exemplo n.º 4
0
Arquivo: db.go Projeto: 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
		},
	}
}
Exemplo n.º 5
0
Arquivo: db.go Projeto: 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)
			}

		},
	}
}