func PrintImage(img image.Image) { width := goterm.Width() height := goterm.Height() img = resize.Resize(uint(width), uint(height), img, resize.NearestNeighbor) buf := "" for y := 0; y < height; y++ { buf += "\n" for x := 0; x < width; x++ { r, g, b, _ := img.At(x, y).RGBA() grayColor := color.Gray16{Y: uint16((r + g + b) / 3)} pixelColor := 232 + (grayColor.Y / 255 / 16) buf += fmt.Sprintf("\033[38;5;#%dm█\033[m", pixelColor) } } goterm.Print(buf) goterm.Flush() }
func listSites(cmd *cobra.Command, args []string) error { c := plumbing.NewHTTPClient(nil) resp, err := c.Operations.ListSites(nil, auth.ClientCredentials()) if err != nil { return err } sites := tm.NewTable(0, 10, 5, ' ', 0) fmt.Fprintf(sites, "SITE\tURL") for _, s := range resp.Payload { fmt.Fprintf(sites, "\n%s\t%s", s.Name, s.URL) } tm.Print(sites) tm.Flush() return nil }
func appList(cmd *cli.Cmd) { cmd.Action = func() { output := tm.NewTable(0, 5, 2, ' ', 0) config := marathon.NewDefaultConfig() config.URL = *marathonHost client, err := marathon.NewClient(config) if err != nil { panic(err) } applications, err := client.Applications(nil) if err != nil { panic(err) } for i, application := range applications.Apps { color := chalk.White health := "" if i != 0 { fmt.Fprint(output, "\n") } if application.HasHealthChecks() { if healthy, _ := client.ApplicationOK(application.ID); healthy { color = chalk.Green health = "Ok" } else { color = chalk.Red health = "Failing" } } fmt.Fprintf(output, "%s%s \t %d/%d/%d \t%s%s", color, application.ID, application.TasksRunning, application.TasksStaged, application.Instances, health, chalk.Reset) } tm.Print(output) tm.Flush() } }
func main() { f, err := os.Create("box.txt") if err != nil { panic("Unable to create box file!") } defer f.Close() // Tell tm to use the file we just opened, not stdout tm.Output = bufio.NewWriter(f) // More or less stolen from the box example tm.Clear() box := tm.NewBox(30|tm.PCT, 20, 0) fmt.Fprint(box, "Some box content") tm.Print(tm.MoveTo(box.String(), 40|tm.PCT, 40|tm.PCT)) tm.Flush() fmt.Println("Now view the contents of 'box.txt' in an ansi-capable terminal") }
func main() { var ( errCh = make(chan error, 16) receivedCh = make(chan int, 1024) sentCh = make(chan int, 1024) sent = 0 lastSent = 0 received = 0 lastReceived = 0 ) // read params (server address:port, qos settings, number of publishers, number of subscribers) flag.Parse() // generate topics in advance, so we can run the subscribers before starting to publish for i := 0; i < *numTop; i++ { newTopic() } // // subscribe to topics // // TODO: multiple subscribers. // initialise a timeout (if no messages are received in the given time since the last publish.) timeout := time.NewTimer(10 * time.Second) resetSubTimeout := func() { timeout.Reset(time.Duration(*subTimeout) * time.Second) } // discarding received messages messageHandler := func(client *mqtt.Client, m mqtt.Message) { if string(m.Payload()) == "hello world" { receivedCh <- 1 resetSubTimeout() // reset timeout } } // prepare filter from topics and qos filters := map[string]byte{} for _, topic := range getTopics() { fmt.Printf("Created topic %s with qos %v\n", topic, *qos) filters[topic] = byte(*qos) } // multisubscribers fmt.Println("Connecting subscribers...") for i := 0; i < *numSub; i++ { subscriber := newClient() if token := subscriber.client.Connect(); token.Wait() && token.Error() != nil { errCh <- token.Error() } defer subscriber.client.Disconnect(250) token := subscriber.client.SubscribeMultiple(filters, messageHandler) if token.Wait() && token.Error() != nil { errCh <- token.Error() } } // // Publish to topics // // set value for timeout timeout = time.NewTimer(time.Duration(*subTimeout) * time.Second) go func() { for { select { case <-timeout.C: errCh <- fmt.Errorf("Subscriber timeout.. no more messages?") } } }() // publishers for i := 0; i < *numPub; i++ { go func() { c := newClient() if token := c.client.Connect(); token.Wait() && token.Error() != nil { errCh <- token.Error() } defer c.client.Disconnect(100) // publish (sequential per client) topics := getTopics() for k := 0; k < *numMessages; k++ { topic := topics[k%len(topics)] token := c.client.Publish(topic, byte(*qos), *retained, "hello world") if token.Wait() && token.Error() != nil { errCh <- token.Error() } sentCh <- 1 time.Sleep(time.Duration(*pubDelay) * time.Millisecond) } }() } // creating fancy output // start := time.Now() redraw := time.NewTicker(100 * time.Millisecond) if *nograph { redraw = time.NewTicker(1 * time.Second) } row := 0.0 // table data := new(tm.DataTable) data.AddColumn("Time (sec)") data.AddColumn("Sent") data.AddColumn("Received") for { select { case <-redraw.C: if !*nograph { tm.Clear() tm.Printf("[Published : Received]: [%v : %v]\n\n", sent, received) row += 1.0 data.AddRow(row/10.0, float64(sent), float64(received)) tm.Print(tm.NewLineChart(tm.Width()-4, tm.Height()-6).Draw(data)) tm.Flush() } else { if sent != lastSent || received != lastReceived { fmt.Printf("[Published : Received]: [%v : %v]\n", sent, received) lastSent, lastReceived = sent, received } } case e := <-errCh: fmt.Println(e.Error()) return case s := <-sentCh: sent += s case r := <-receivedCh: received += r } } }
func appInfo(cmd *cli.Cmd) { cmd.Spec = "NAME" var ( name = cmd.StringArg("NAME", "", "") ) cmd.Action = func() { config := marathon.NewDefaultConfig() config.URL = *marathonHost client, err := marathon.NewClient(config) if err != nil { fmt.Print("Can't connect to marathon\n") os.Exit(1) } application, err := client.Application(*name) if err != nil { fmt.Print("Application doesn't exists\n") os.Exit(1) } output := tm.NewTable(0, 2, 1, ' ', 0) //output := os.Stdout fmt.Fprint(output, "\n") fmt.Fprintf(output, "Application Name: \t\"%s\"\n", application.ID) fmt.Fprintf(output, "Running/Staged/Failing/Requested: \t%d/%d/%d/%d\n", application.TasksRunning, application.TasksStaged, application.TasksUnhealthy, application.Instances) if application.TasksRunning > 0 { fmt.Fprint(output, "\nRunning tasks:\n") for _, task := range application.Tasks { if application.HasHealthChecks() { var alive bool for _, result := range task.HealthCheckResult { alive = result.Alive } // TODO: Support multiple ports if alive { fmt.Fprintf(output, "%s - %s:%d \t Ok%s\n", chalk.Green, task.Host, task.Ports[0], chalk.Reset) } else { fmt.Fprintf(output, "%s - %s:%d \t Failing%s\n", chalk.Red, task.Host, task.Ports[0], chalk.Reset) } } else { fmt.Fprintf(output, " - %s:%d\n", task.Host, task.Ports[0]) } } } else { fmt.Fprint(output, " - Exposed ports: \tNo\n") } fmt.Fprint(output, "\nHealthcheck Information:\n") fmt.Fprintf(output, " - Has Healthcheck? : \t%t\n", application.HasHealthChecks()) if application.HasHealthChecks() { if healthy, _ := client.ApplicationOK(application.ID); healthy { fmt.Fprintf(output, " - Healthcheck Status: \t%sOk%s\n", chalk.Green, chalk.Reset) } else { fmt.Fprintf(output, " - Healthcheck Status: \t%sFailing%s\n", chalk.Red, chalk.Reset) } for i, healthcheck := range application.HealthChecks { fmt.Fprintf(output, "\nHealthcheck num: %d\n", i+1) fmt.Fprintf(output, " - Protocol: \t%s\n", healthcheck.Protocol) if healthcheck.Protocol == "COMMAND" { fmt.Fprintf(output, " - Command: \t%s\n", healthcheck.Command.Value) } else { fmt.Fprintf(output, " - Path: \t%s\n", healthcheck.Path) } fmt.Fprintf(output, " - Grace period seconds: \t%ds\n", healthcheck.GracePeriodSeconds) fmt.Fprintf(output, " - Interval: \t%ds\n", healthcheck.IntervalSeconds) fmt.Fprintf(output, " - Timeout: \t%ds\n", healthcheck.TimeoutSeconds) fmt.Fprintf(output, " - Max consecutive failures: \t%d\n", healthcheck.MaxConsecutiveFailures) } } tm.Print(output) tm.Flush() output = tm.NewTable(0, 2, 1, ' ', 0) taskRunningF := float64(application.TasksRunning) fmt.Fprint(output, "Per instance limits:\n") fmt.Fprintf(output, " - CPU shares: \t%.1f \t(%.1f)\n", application.CPUs, application.CPUs*taskRunningF) fmt.Fprintf(output, " - Memory: \t%.1f \t(%.1f)\n", application.Mem, application.Mem*taskRunningF) fmt.Fprintf(output, " - Disk: \t%.1f \t(%.1f)\n", application.Disk, application.Disk*taskRunningF) fmt.Fprint(output, "\nUpgrade strategy:\n") fmt.Fprintf(output, " - Maximum over capacity: \t%3.f%% \n", application.UpgradeStrategy.MaximumOverCapacity*100) fmt.Fprintf(output, " - Minimum health capacity: \t%3.f%% \n", application.UpgradeStrategy.MinimumHealthCapacity*100) fmt.Fprint(output, "\nEnv vars:\n") for k := range application.Env { fmt.Fprintf(output, " - %s = \"%s\" \n", k, application.Env[k]) } tm.Print(output) tm.Flush() } }
func appUpdate(cmd *cli.Cmd) { cmd.Spec = "NAME [--instances=<num> | --cpu=<num> |--mem=<num> | --docker-image=<image>]" // healthCheckCMD = cmd.StringOpt("command-healthcheck", "", "Command to use as healthcheck") // healthCheckHTTP = cmd.StringOpt("http-healthcheck", "", "HTTP path to use as healthcheck") // healthCheckTCP = cmd.IntOpt("tcp-healthcheck", -1, "TCP port to use as healthcheck") var ( instances = cmd.IntOpt("instances", 0, "Number of instances") dockerImage = cmd.StringOpt("docker-image", "", "Docker image and version") cpu = cmd.StringOpt("cpu", "", "cpu shares") mem = cmd.StringOpt("mem", "", "memory mb limit") name = cmd.StringArg("NAME", "", "Application name") ) cmd.Action = func() { config := marathon.NewDefaultConfig() config.URL = *marathonHost client, err := marathon.NewClient(config) application, err := client.Application(*name) application.Version = "" output := tm.NewTable(0, 2, 1, ' ', 0) // if *healthCheckCMD != "" { // // } // // if *healthCheckHTTP != "" { // // } // // if *healthCheckTCP != -1 { // // } if *dockerImage != "" { fmt.Fprintf(output, "Setting new docker image:\t %s \t-> %s\n", application.Container.Docker.Image, *dockerImage) application.Container.Docker.Container(*dockerImage) } if *cpu != "" { cpuFloat, _ := strconv.ParseFloat(*cpu, 64) fmt.Fprintf(output, "Setting new cpu limits:\t %.3f \t-> %.3f\n", application.CPUs, cpuFloat) application.CPU(cpuFloat) if err != nil { panic(err) } } if *mem != "" { memFloat, _ := strconv.ParseFloat(*mem, 64) fmt.Fprintf(output, "Setting new memory limits:\t %.1f \t-> %.1f\n", application.Mem, memFloat) application.Memory(memFloat) if err != nil { panic(err) } } if *instances != 0 { fmt.Fprintf(output, "Setting new number of instances:\t %d \t-> %d\n", application.Instances, *instances) application.Count(*instances) } deployment, err := client.UpdateApplication(application) if err != nil { panic(err) } tm.Print(output) tm.Flush() fmt.Fprintf(os.Stdout, "Starting deployment: %s\n", deployment.DeploymentID) client.WaitOnApplication(application.ID, 60*time.Second) fmt.Println("Application deployed!") } }