Exemple #1
0
func listParams() cli.Command {
	return cli.Command{
		Name:  "list",
		Usage: "list of parametrs",
		Flags: []cli.Flag{
			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) {
			logger.Trace.Println("command params list")
			defer logger.Trace.Println("command params list done")
			d := parameters.GetInstance()
			defer checkErr(d.Close)
			params, err := d.All()
			if err != nil {
				panic(err)
			}
			tab := fmttab.New("List of databases", fmttab.BorderThin, nil)

			tab.AddColumn("Id", 4, fmttab.AlignRight)
			tab.AddColumn("Name", fmttab.WidthAuto, fmttab.AlignLeft)
			tab.AddColumn("Value", fmttab.WidthAuto, fmttab.AlignLeft)
			tab.AddColumn("Description", fmttab.WidthAuto, fmttab.AlignLeft)
			for _, curd := range params.Get() {
				rec := make(map[string]interface{})
				rec["Id"] = curd.ID
				rec["Name"] = curd.Name
				rec["Value"] = curd.ValueStr()
				rec["Description"] = curd.Description
				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)
			}

			if _, err := tab.WriteTo(os.Stdout); err != nil {
				panic(err)
			}
		},
	}
}
Exemple #2
0
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
		},
	}
}
Exemple #3
0
func createParametersGetter(ctx *cli.Context) parametergetter.ParameterGetter {
	return parametergetter.New(ctx, parameters.GetInstance())
}
Exemple #4
0
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)
			}

		},
	}
}