func main() { fmt.Println("Simple table:") t := termtable.NewTable(nil, nil) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) fmt.Println("\nSimple table, alternative syntax:") rows := [][]string{ []string{"abc", "ABCD", "12345"}, []string{"defg", "EFGHI", "678"}, []string{"hijkl", "JKL", "9000"}, } t = termtable.NewTable(rows, nil) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) fmt.Println(t.Render()) fmt.Println("\nSimple table w/ separators and custom padding:") t = termtable.NewTable(nil, &termtable.TableOptions{ Padding: 3, UseSeparator: true, }) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) }
func formatResults(results [][]interface{}, columns []string) string { rows := [][]string{} for _, row := range results { s := []string{} for _, res := range row { if res == nil { s = append(s, "NULL") } else { s = append(s, res.(string)) } } rows = append(rows, s) } t := termtable.NewTable(rows, &termtable.TableOptions{Padding: 2, UseSeparator: true}) t.SetHeader(columns) return t.Render() }
func CmdProfiles(c *Config) *cobra.Command { return &cobra.Command{ Use: "profiles", Short: "Manage entity profiles.", Long: ` List, add or remove profiles or change the default. Profiles save entity uris' and credentials. They are identified by a unique name. The default profile is used by other commands like create, query and get.`, Run: func(cmd *cobra.Command, args []string) { t := termtable.NewTable(nil, nil) t.SetHeader([]string{"NAME", "ENTITY", "ID", "KEY", "APP"}) for _, p := range c.Profiles { t.AddRow([]string{p.Name, p.Entity, p.ID, p.Key, p.App}) } fmt.Println(t.Render()) }, } }
func CmdSchemas(c *Config) *cobra.Command { return &cobra.Command{ Use: "schemas", Short: "Manage post schemas.", Long: ` List, set or remove post schemas. Schemas keep you from typing post types over and over by mapping them to short names. This simple feature might get expanded when strict post schemas get introduced with Tent 0.4. Post types can optionally be saved with fragments (e.g. "schemas add status https://tent.io/types/status/v0#reply"). These extensions can be overwritten when used: "create status#different_fragment". `, Run: func(cmd *cobra.Command, args []string) { t := termtable.NewTable(nil, nil) t.SetHeader([]string{"NAME", "POSTTYPE"}) for _, s := range c.Schemas { t.AddRow([]string{s.Name, s.PostType}) } fmt.Println(t.Render()) }, } }
func main() { // arg from cmdline flag.Usage = func() { fmt.Printf("Usage: findilo [options] ww.xx.yy.zz/aa ...\n ww.xx.yy.zz/aa - CIDR network address\n\nOptions:\n") flag.PrintDefaults() } flag.Parse() // Create ip list var hosts []host for _, argNet := range flag.Args() { _, subnet, err := net.ParseCIDR(argNet) if err != nil { log.Fatal(err) } ip := subnet.IP for subnet.Contains(ip) { hosts = append(hosts, host{ip: ip}) ip = ipInc(ip) } } var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Fatal("Error Getting Rlimit ", err) } wNum := int(rLimit.Cur / 2) hNum := len(hosts) bar := pb.StartNew(hNum) bar.ShowTimeLeft = false // Create workers jobs := make(chan int, hNum) for w := 0; w < wNum; w++ { go httpWorker(jobs, hosts, bar) } for i := range hosts { wg.Add(1) jobs <- i } close(jobs) wg.Wait() //create table and fill it table := termtable.NewTable(nil, &termtable.TableOptions{ Padding: 3, UseSeparator: true, }) header := []string{"ip", "Model", "Serial", "iLO", "Firmware", "Build date", "Error"} if *verbose { table.SetHeader(header) } else { table.SetHeader(header[:len(header)-1]) } for _, h := range hosts { row := []string{h.ip.String(), h.SPN, h.SBSN, h.PN, h.FWRI, h.BBLK, h.Err} if *verbose { table.AddRow(row) } else { if h.PN != "" { table.AddRow(row[:len(row)-1]) } } } bar.Finish() fmt.Println(table.Render()) }
func CmdQuery(c *Config) *cobra.Command { var limit int var since string // 1234567890,version var before string // 1234567890,version var until string // 1234567890,version var entities string // entityone,entitytwo var types string // typeone,typetwo var maxRefs int var sortBy string // r,p,vr,vp // var mentions string // mentionone,mentiontwo var profile string cmd := &cobra.Command{ Use: "query", Short: "Query the posts feed.", Long: ` Query the posts feed of the default profile. Find more information about the available parameters here: https://tent.io/docs/api#postsfeed Join multiple values with commata, i.e. when using --entities or --types. A note about --types and fragments: - "--types=https://tent.io/types/status/v0" matches all fragments - "--types=https://tent.io/types/status/v0#" just matches "https://tent.io/types/status/v0#" - "--types=https://tent.io/types/status/v0#reply" just matches "https://tent.io/types/status/v0#reply" `, Run: func(cmd *cobra.Command, args []string) { p, err := c.Profile(profile) maybeExit(err) client := p.Client() q := tent.NewPostsFeedQuery() q.Limit(limit) q.MaxRefs(maxRefs) timeSetter := map[string]func(time.Time, string) *tent.PostsFeedQuery{ since: q.Since, before: q.Before, until: q.Until, } for arg, setter := range timeSetter { err = splitAndSetTimeValue(arg, setter) maybeExit(err) } if entities != "" { entitiesStr, err := splitAndMaybeReplace(entities, func(name string) (string, error) { if i, p := c.ProfileByName(name); i > -1 { return p.Entity, nil } if !isURL(name) { return "", fmt.Errorf(`Profile "%v" not found.`, name) } return name, nil }) maybeExit(err) q.Set("entities", entitiesStr) } if types != "" { typesStr, err := splitAndMaybeReplace(types, func(name string) (string, error) { if i, s := c.SchemaByName(name); i > -1 { return s.MergeFragment(name), nil } if !isURL(name) { return "", fmt.Errorf(`Schema "%v" not found.`, name) } return name, nil }) maybeExit(err) q.Set("types", typesStr) } if sortBy != "" { order, err := matchSortOrder(sortBy) maybeExit(err) q.SortBy(order) } res, err := client.GetFeed(q, nil) maybeExit(err) t := termtable.NewTable(nil, nil) t.SetHeader([]string{"ID", "ENTITY", "TYPE", "PUBLISHED_AT"}) for _, p := range res.Posts { // layout := "2006-01-02 15:04" // p.PublishedAt.Format(layout) pubAt := strconv.FormatInt(p.PublishedAt.UnixMillis(), 10) t.AddRow([]string{p.ID, p.Entity, p.Type, pubAt}) } fmt.Println(t.Render()) }, } cmd.Flags().IntVarP(&limit, "limit", "l", 25, "Cap number of returned posts.") cmd.Flags().StringVarP(&since, "since", "s", "", "Only posts since timestamp and/or version. Format: `123456789,versionid`") cmd.Flags().StringVarP(&before, "before", "b", "", "Only posts before timestamp and/or version. Format: `123456789,versionid`") cmd.Flags().StringVarP(&until, "until", "u", "", "Only posts until timestamp and/or version. Format: `123456789,versionid`") cmd.Flags().StringVarP(&entities, "entities", "e", "", "Only posts from specific entities.") cmd.Flags().StringVarP(&types, "types", "t", "", "Only posts with specific types.") cmd.Flags().IntVarP(&maxRefs, "maxrefs", "r", 5, "Cap number of inlined refs per post.") cmd.Flags().StringVarP(&sortBy, "sortBy", "o", "", `Define sort order. Possible values: "p" published_at, "r" received_at, "vp" version.published_at, "vr" version.received_at`) // cmd.Flags().StringVarP(&mentions, "mentions", "m", "", "Only posts which mention specific entities.") setUseFlag(&profile, cmd) return cmd }
func main() { services := []string{ "cronie.service", "httpd.service", "mysqld.service", "ntpd.service", "postfix.service", "sshd.service", "home.mount", "mnt-extra.mount", "tmp.mount", "var-lib-mysqltmp.mount", "var.mount", } t := termtable.NewTable(nil, nil) t.SetHeader([]string{"SERVICE", "STATUS"}) for _, service := range services { // The systemctl command. syscommand := exec.Command("systemctl", "status", service) // The grep command. grepcommand := exec.Command("grep", "Active:") // Pipe the stdout of syscommand to the stdin of grepcommand. grepcommand.Stdin, _ = syscommand.StdoutPipe() // Create a buffer of bytes. var b bytes.Buffer // Assign the address of our buffer to grepcommand.Stdout. grepcommand.Stdout = &b // Start grepcommand. _ = grepcommand.Start() // Run syscommand _ = syscommand.Run() // Wait for grepcommand to exit. _ = grepcommand.Wait() s := fmt.Sprintf("%s", &b) if strings.Contains(s, "active (running)") || strings.Contains(s, "active (mounted)") { color := ansi.ColorCode("green+h:black") reset := ansi.ColorCode("reset") t.AddRow([]string{fmtString(color, service, reset), fmtString(color, s, reset)}) } else { color := ansi.ColorCode("red+h:black") reset := ansi.ColorCode("reset") t.AddRow([]string{fmtString(color, service, reset), fmtString(color, s, reset)}) } } fmt.Println(t.Render()) }
func main() { fmt.Println("Simple table:") t := termtable.NewTable(nil, nil) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) fmt.Println("\nSimple table, alternative syntax:") rows := [][]string{ []string{"abc", "ABCD", "12345"}, []string{"defg", "EFGHI", "678"}, []string{"hijkl", "JKL", "9000"}, } t = termtable.NewTable(rows, nil) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) fmt.Println(t.Render()) fmt.Println("\nSimple table w/ separators and custom padding:") t = termtable.NewTable(nil, &termtable.TableOptions{ Padding: 3, UseSeparator: true, }) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) fmt.Println("\nSimple table w/ seperators, custom padding, and dynamic rows:") t = termtable.NewTable(nil, &termtable.TableOptions{ Padding: 3, UseSeparator: true, }) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) t.AddRow([]string{"mnop", "MNO", "479108"}) fmt.Println(t.RenderDynamic()) fmt.Println("\nSimple table w/ seperators, custom padding, dynamic rows, and column wrapping:") t = termtable.NewTable(nil, &termtable.TableOptions{ Padding: 3, UseSeparator: true, }) t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"}) t.AddRow([]string{"abc", "ABCD", "12345"}) t.AddRow([]string{"defg", "EFGHI", "678"}) t.AddRow([]string{"hijkl", "JKL", "9000"}) fmt.Println(t.Render()) t.AddRow([]string{"mnop", "MASDADADNO", "47910987978"}) t.AddRow([]string{"qrstuvwxyz", "QRS", "54234"}) fmt.Println(t.RenderDynamic()) }