Пример #1
0
func (ctx *CommandContext) addSshKeys(c *cobra.Command, args []string) {
	// validate that arguments for locators are passsed
	if len(args) < 1 {
		cmd.Fail(1, "Valid arguments: <id> ...")
	}

	t := ctx.Transport.Get()

	// args... are locators for repositories or containers
	ids, err := cmd.NewResourceLocators(t, cloc.ResourceTypeContainer, args...)
	if err != nil {
		cmd.Fail(1, "You must pass 1 or more valid names: %s", err.Error())
	}

	keys, err := readAuthorizedKeysFile(ctx.keyFile)
	if err != nil {
		cmd.Fail(1, "Unable to read authorized keys file: %s", err.Error())
	}

	allPerms := make(map[string]*jobs.KeyPermission)
	for i := range ids {
		resourceType := ids[i].(*cmd.ResourceLocator).Type
		if permissionHandlers == nil {
			cmd.Fail(1, "The type '%s' is not supported by this command", resourceType)
		}
		h, ok := permissionHandlers[resourceType]
		if !ok {
			cmd.Fail(1, "The type '%s' is not supported by this command", resourceType)
		}
		perm, err := h.CreatePermission(c, ids[i].(*cmd.ResourceLocator).Id)
		if err != nil {
			cmd.Fail(1, err.Error())
		}
		allPerms[ids[i].Identity()] = perm
	}

	cmd.Executor{
		On: ids,
		Group: func(on ...cmd.Locator) cmd.JobRequest {
			permissions := []jobs.KeyPermission{}
			for i := range on {
				permissions = append(permissions, *allPerms[on[i].Identity()])
			}

			return &jobs.CreateKeysRequest{
				&jobs.ExtendedCreateKeysData{
					Keys:        keys,
					Permissions: permissions,
				},
			}
		},
		Output: os.Stdout,
		//TODO: display partial error info
		Transport: t,
	}.StreamAndExit()
}
Пример #2
0
func TestShouldReadIdentifiersFromArgs(t *testing.T) {
	ids, err := cmd.NewResourceLocators(&testTransport{}, ResourceTypeContainer, "ctr://localhost/foo", "bart", "ctr://local/bazi")
	if err != nil {
		t.Errorf("No error should occur reading locators: %s", err.Error())
	}
	if len(ids) != 3 {
		t.Errorf("Should have received 3 ids: %+v", ids)
	}
	if string(AsIdentifier(ids[0])) != "" {
		t.Error("First id should have value '' because foo is too short", ids[0])
	}
	if string(AsIdentifier(ids[1])) != "bart" {
		t.Error("Second id should have value 'bart'", ids[1])
	}
	if string(AsIdentifier(ids[2])) != "bazi" {
		t.Error("Third id should have value 'bazi'", ids[2])
	}
}