Ejemplo n.º 1
0
Archivo: pvc.go Proyecto: pipeviz/pvc
func validateAndPrint(w io.Writer, v interface{}) {
	msg, err := toJSONBytes(v)
	if err != nil {
		fmt.Fprintf(w, err.Error())
		return
	}

	// Validate the current state of the message
	result, err := schemaMaster.Validate(gojsonschema.NewStringLoader(string(msg)))
	if err != nil {
		fmt.Fprintf(w, "\nError while attempting to validate data: %s\n", err.Error())
		return
	}
	if !result.Valid() {
		fmt.Fprintln(w, "\nAs it stands now, the data will fail validation if sent to a pipeviz server. Errors:")
		for _, desc := range result.Errors() {
			fmt.Fprintf(w, "\t%s\n", desc)
		}
	}
}
Ejemplo n.º 2
0
Archivo: ls.go Proyecto: pipeviz/pvc
// runGenLS is the main entry point for running the logic state-generating
// ls subcommand.
func (lsc lsCmd) runGenLS(cmd *cobra.Command, args []string) {
	ls := &semantic.LogicState{}

	if !cmd.Flags().Lookup("no-detect").Changed {
		*ls = lsc.detectDefaults()
	}

	// Write directly to stdout, at least for now
	w := os.Stdout

	// Prep schema to validate the messages as we go
	raw, err := schema.Master()
	if err != nil {
		fmt.Fprintln(w, "WARNING: Failed to open master schema file; pvc cannot validate outgoing messages.")
	}

	schemaMaster, err = gjs.NewSchema(gjs.NewStringLoader(string(raw)))
	if err != nil {
		panic("bad schema...?")
	}

	client := http.Client{Timeout: 5 * time.Second}

	fmt.Fprintln(w, "Generating a logic state message...")
	reader := bufio.NewReader(os.Stdin)
MenuLoop:
	for {
		fmt.Fprintf(w, "\n")
		lsc.printCurrentState(w, *ls)

		var input string
		for {
			fmt.Fprintf(w, "\nSelect a value to edit by number, (p)rint current JSON message, (s)end, or (q)uit: ")
			l, err := fmt.Fscanln(reader, &input)
			if l > 1 || err != nil {
				continue
			}

			switch input {
			case "q", "quit":
				fmt.Fprintf(w, "\nQuitting; message was not sent\n")
				os.Exit(1)
			case "s", "send":
				msg, err := toJSONBytes(*ls)
				if err != nil {
					log.Fatalf("\nFailed to marshal JSON of logic state object, no message sent\n")
				}

				resp, err := client.Post(cmd.Flags().Lookup("target").Value.String(), "application/json", bytes.NewReader(msg))
				if err != nil {
					log.Fatalf(err.Error())
				}

				bod, err := ioutil.ReadAll(resp.Body)
				resp.Body.Close()
				if err != nil {
					log.Fatalf(err.Error())
				}

				if resp.StatusCode >= 200 && resp.StatusCode <= 300 {
					fmt.Printf("Message accepted (HTTP code %v), msgid %v\n", resp.StatusCode, string(bod))
				} else {
					fmt.Printf("Message was rejected with HTTP code %v and message %v\n", resp.StatusCode, string(bod))
				}
				break MenuLoop

			case "p", "print":
				byts, err := toJSONBytes(*ls)
				if err != nil {
					fmt.Fprintf(w, "Error while generating JSON for printing: %q", err.Error())
					continue MenuLoop
				}

				var prettied bytes.Buffer
				err = json.Indent(&prettied, byts, "", "    ")
				if err != nil {
					fmt.Fprintf(w, "Error while generating JSON for printing: %q", err.Error())
					continue MenuLoop
				}

				fmt.Fprintf(w, "\nMessage that will be sent to %s:\n", cmd.Flags().Lookup("target").Value)
				prettied.WriteTo(w)
				w.WriteString("\n")

			default:
				num, interr := strconv.Atoi(input)
				if interr != nil {
					continue
				} else if 0 < num && num < 10 {
					switch num {
					case 1:
						lsc.collectPath(w, reader, ls)
					case 2:
						lsc.collectHostFQDN(w, reader, ls)
					case 3:
						lsc.collectHostNick(w, reader, ls)
					case 4:
						lsc.collectCommit(w, reader, ls)
					case 5:
						lsc.collectVersion(w, reader, ls)
					case 6:
						lsc.collectSemver(w, reader, ls)
					case 7:
						lsc.collectLgroup(w, reader, ls)
					case 8:
						lsc.collectNick(w, reader, ls)
					case 9:
						lsc.collectType(w, reader, ls)
					}
					continue MenuLoop
				} else {
					continue
				}
			}
		}
	}
}