~w1ke/hut

3d0cba51b37d2a52916f47795ed64cfbe6f019ba — w1ke 11 months ago 60f3b3b
added hut git config
2 files changed, 112 insertions(+), 0 deletions(-)

M api/git/git.go
M cmd/hut/git.go
M api/git/git.go => api/git/git.go +70 -0
@@ 194,3 194,73 @@ func repoInfo(r *Repository) {
	}
	fmt.Println()
}

func (c *Client) getRepo(identificator string) (*Repository, error) {
	if userRepoRegex.MatchString(identificator) {
		slice := strings.Split(identificator, "/")
		username, reponame := slice[0], slice[1]

		var query struct {
			RepositoryByOwner *Repository `graphql:"repositoryByOwner(owner: $owner, repo: $repo)"`
		}

		variables := map[string]interface{}{
			"owner": graphql.String(username),
			"repo":  graphql.String(reponame),
		}

		err := c.graphql.Query(context.Background(), &query, variables)
		if err != nil {
			return nil, err
		}

		if query.RepositoryByOwner == nil {
			return nil, errors.New("repository not found")
		}

		return query.RepositoryByOwner, nil
	} else if meRepoRegex.MatchString(identificator) {
		var query struct {
			RepositoryByName *Repository `graphql:"repositoryByName(name: $name)"`
		}

		variables := map[string]interface{}{
			"name": graphql.String(identificator),
		}

		err := c.graphql.Query(context.Background(), &query, variables)
		if err != nil {
			return nil, err
		}

		return query.RepositoryByName, nil
	} else {
		return nil, errors.New("invalid identificator")
	}
}

type RepoInput struct {
	Name        *graphql.String `json:"name,omitempty"`
	Description *graphql.String `json:"description,omitempty"`
	Visibility  *Visibility     `json:"visibility,omitempty"`
	Readme      *graphql.String `json:"readme,omitempty"`
}

// TODO: visibility setting nonfunctional
func (c *Client) Config(identificator string, input RepoInput) error {
	repo, err := c.getRepo(identificator)
	if err != nil {
		return err
	}

	var mutation struct {
		UpdateRepository Repository `graphql:"updateRepository(id: $id, input: $input)"`
	}

	variables := map[string]interface{}{
		"id":    (graphql.Int)(repo.Id),
		"input": input,
	}

	return c.graphql.Mutate(context.Background(), &mutation, variables)
}

M cmd/hut/git.go => cmd/hut/git.go +42 -0
@@ 9,6 9,7 @@ import (
	"strings"

	"git.sr.ht/~w1ke/hut/api/git"
	"github.com/shurcooL/graphql"
)

func gitRoot(args []string) {


@@ 62,6 63,47 @@ func gitRoot(args []string) {
			log.Fatal(err)
		}
	case "config":
		args = args[1:]
		if len(args) == 0 {
			gitUsage()
		}
		identificator := args[0]
		flagSet := flag.NewFlagSet("hut git config", flag.ExitOnError)
		var (
			name        = flagSet.String("name", "", "new name of the repo")
			description = flagSet.String("description", "", "new description of the repo")
			visibility  git.Visibility
			readme      = flagSet.String("readme", "", "the new readme of the repo")
		)
		flagSet.Func("visibility", "the new visibility of the repo", func(s string) error {
			switch strings.ToLower(s) {
			case "public":
			case "unlisted":
			case "private":
			default:
				return errors.New("invalid visibility")
			}
			visibility = git.Visibility(strings.ToUpper(s))
			return nil
		})
		flagSet.Parse(args[1:])
		var repoInput git.RepoInput
		if isFlagPassed(flagSet, "name") {
			repoInput.Name = (*graphql.String)(name)
		}
		if isFlagPassed(flagSet, "description") {
			repoInput.Description = (*graphql.String)(description)
		}
		if isFlagPassed(flagSet, "visibility") {
			repoInput.Visibility = &visibility
		}
		if isFlagPassed(flagSet, "readme") {
			repoInput.Readme = (*graphql.String)(readme)
		}
		err := client.Config(identificator, repoInput)
		if err != nil {
			log.Fatal(err)
		}
	case "delete":
	case "permit":
	case "deny":