Пример #1
0
// Link allows the links to be created on the Index
func (s SpreadCli) Link() *cli.Command {
	return &cli.Command{
		Name:        "link",
		Usage:       "spread link <target-url> <attach-point>",
		Description: "Create/remove links on Index",
		Action: func(c *cli.Context) {
			targetUrl := c.Args().First()
			if len(targetUrl) == 0 {
				s.fatalf("A target URL must be specified")
			}

			target, err := data.ParseSRI(targetUrl)
			if err != nil {
				s.fatalf("Error using target: %v", err)
			}

			attachPoint := c.Args().Get(1)
			if len(attachPoint) == 0 {
				s.fatalf("An attach point must be specified")
			}

			attach, err := data.ParseSRI(attachPoint)
			if err != nil {
				s.fatalf("Error using attach-point: %v", err)
			}

			proj := s.projectOrDie()
			index, err := proj.Index()
			if err != nil {
				s.fatalf("Error retrieving index: %v", err)
			}

			doc, ok := index[attach.Path]
			if !ok {
				s.fatalf("Path '%s' not found", attach.Path)
			}

			link := data.NewLink("test", target, false)
			if err = data.CreateLinkInDocument(doc, link, attach); err != nil {
				s.fatalf("Could not create link: %v", err)
			}

			if err = proj.AddDocumentToIndex(doc); err != nil {
				s.fatalf("Failed to add object to Git index: %v", err)
			}
		},
	}
}
Пример #2
0
// Param allows the parameters to be created on the Index
func (s SpreadCli) Param() *cli.Command {
	return &cli.Command{
		Name:      "param",
		Usage:     "Set paramaters for field values in the index",
		ArgsUsage: "<SRL> <name> <prompt>",
		Flags: []cli.Flag{
			cli.BoolFlag{
				Name:  "l",
				Usage: "list parameters",
			},
			cli.StringFlag{
				Name:  "f",
				Usage: "set Golang format string to use with arguments",
			},
			cli.StringFlag{
				Name:  "d",
				Usage: "set default value, interpretted as JSON",
			},
		},
		Action: func(c *cli.Context) {
			if c.Bool("l") {
				p := s.projectOrDie()
				docs, err := p.Index()
				if err != nil {
					s.fatalf("Could not retrieve index: %v", err)
				}

				paramFields := data.ParameterFields(docs)
				for _, field := range paramFields {
					param := field.GetParam()
					s.printf(" - Name: %s", param.Name)
					s.printf("   Description: %s", param.Prompt)
					s.printf("   Pattern: %s", param.Pattern)
					if param.GetDefault() == nil {
						s.printf("   Required: Yes")

					}
				}
				return
			}

			if len(c.Args()) < 3 {
				s.fatalf("an srl, name, and description must be provided")
			}

			targetUrl := c.Args().First()
			if len(targetUrl) == 0 {
				s.fatalf("A target SRI must be specified")
			}

			target, err := data.ParseSRI(targetUrl)
			if err != nil {
				s.fatalf("Error using target: %v", err)
			}

			proj := s.projectOrDie()
			doc, err := proj.DocFromIndex(target.Path)
			if err != nil {
				s.fatalf("Error retrieving from index: %v", err)
			}

			param := &pb.Parameter{
				Name:    c.Args().Get(1),
				Prompt:  c.Args().Get(2),
				Pattern: c.String("f"),
			}

			// parse default value
			defaultInput := c.String("d")
			if len(defaultInput) != 0 {
				args, err := data.ParseArguments(defaultInput, false)
				if err != nil {
					s.fatalf("Could not parse default value: %v", err)
				} else if len(args) > 1 {
					s.fatalf("Only one default value can be specified")
				}
				param.Default = args[0]
			}

			if err = data.AddParamToDoc(doc, target, param); err != nil {
				s.fatalf("Failed to add parameter: %v", err)
			}

			if err = proj.AddDocumentToIndex(doc); err != nil {
				s.fatalf("Failed to add object to Git index: %v", err)
			}
		},
	}
}