コード例 #1
0
ファイル: main.go プロジェクト: sbinet/go-github-client
func main() {
	help := flag.Bool("help", false, "Show usage")
	username := flag.String("u", "", "Specify Github user")
	password := flag.String("p", "", "Specify Github password")

	flag.Usage = func() {
		fmt.Printf("Usage:\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	if *help == true || *username == "" || *password == "" {
		flag.Usage()
		return
	}

	ghc, err := ghclient.NewGithubClient(*username, *password, ghclient.AUTH_USER_PASSWORD)

	gistsc := ghgists.NewGists(ghc)

	res, err := gistsc.GetPublicGistsList()

	jr, err := res.Json()

	fmt.Printf("JSON: %v\nERROR: %v\n", jr, err)

	print("\n\nLOADING NEXT PAGE...\n\n")
	res, err = res.GetNextPage()

	jr, err = res.Json()

	fmt.Printf("JSON: %v\nERROR: %v\n", jr, err)
}
コード例 #2
0
ファイル: main.go プロジェクト: sbinet/go-github-client
func main() {
	help := flag.Bool("help", false, "Show usage")
	username := flag.String("u", "", "Specify Github user")
	password := flag.String("p", "", "Specify Github password")
	userinfo := flag.String("userinfo", "", "Specify another Github user")

	flag.Usage = func() {
		fmt.Printf("Usage:\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	if *help == true || *username == "" || *password == "" || *userinfo == "" {
		flag.Usage()
		return
	}

	ghc, err := ghclient.NewGithubClient(*username, *password,
		ghclient.AUTH_USER_PASSWORD)

	ghusersc := ghusers.NewUsers(ghc)

	res, err := ghusersc.GetUserInfo(*userinfo)

	jr, err := res.Json()

	fmt.Printf("JSON: %v\nHTTP REPLY: %v\nERROR: %v\n", jr, res.RawHttpResponse, err)
}
コード例 #3
0
ファイル: cmd_dl_rm.go プロジェクト: sbinet/goctogit
func git_run_cmd_dl_rm(cmd *commander.Command, args []string) {
	n := "github-" + cmd.Name()
	if len(args) != 1 {
		err := fmt.Errorf("%s: needs a file-id to delete", n)
		handle_err(err)
	}
	file_id := args[0]

	repo_name := cmd.Flag.Lookup("repo").Value.Get().(string)
	if repo_name == "" {
		err := fmt.Errorf("%s: needs a github repository name to delete from", n)
		handle_err(err)
	}

	user := cmd.Flag.Lookup("u").Value.Get().(string)
	org := cmd.Flag.Lookup("org").Value.Get().(string)

	if user == "" {
		v, err := Cfg.String("go-octogit", "username")
		handle_err(err)
		user = v
	}

	password, err := Cfg.String("go-octogit", "password")
	handle_err(err)

	ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
	handle_err(err)

	account := user
	// DELETE /repos/:owner/:repo/downloads/:id
	if org != "" {
		account = org
	}
	url := path.Join("repos", account, repo_name, "downloads", file_id)

	fmt.Printf("%s: deleting download id=%s from [%s/%s]...\n",
		n, file_id, account, repo_name)

	req, err := ghc.NewAPIRequest("DELETE", url, nil)
	handle_err(err)

	resp, err := ghc.RunRequest(req, new(http.Client))
	handle_err(err)

	sc := resp.RawHttpResponse.StatusCode
	switch sc {
	case 204:
		// all good
	case 404:
		err = fmt.Errorf("%s: no such file-id\n", n)
	default:
		err = fmt.Errorf("%s: request did not succeed. got (status=%d) %v\n", n, resp.RawHttpResponse.StatusCode, resp.RawHttpResponse)
	}
	handle_err(err)

	fmt.Printf("%s: deleting download id=%s from [%s/%s]... [done]\n",
		n, file_id, account, repo_name)
}
コード例 #4
0
ファイル: cmd_login.go プロジェクト: sbinet/goctogit
func git_run_cmd_login(cmd *commander.Command, args []string) {
	n := "github-" + cmd.Name()

	user := cmd.Flag.Lookup("u").Value.Get().(string)
	fmt.Printf("github username: "******"" {
		_, err := fmt.Scanln(&user)
		handle_err(err)
	} else {
		fmt.Printf("%s\n", user)
	}

	password, err := getpasswd("github password: "******"go-octogit"
	for k, v := range map[string]string{
		"username": user,
		"password": password,
	} {
		if Cfg.HasOption(section, k) {
			Cfg.RemoveOption(section, k)
		}
		if !Cfg.AddOption(section, k, v) {
			err := fmt.Errorf("%s: could not add option [%s] to section [%s]", n, k, section)
			panic(err.Error())
		}
	}

	// check credentials
	ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
	handle_err(err)

	req, err := ghc.NewAPIRequest("GET", "authorizations", nil)
	handle_err(err)

	resp, err := ghc.RunRequest(req, new(http.Client))
	handle_err(err)

	if !resp.IsSuccess() {
		err = fmt.Errorf("%s: authentication failed\n%v\n", n, resp.RawHttpResponse)
		handle_err(err)
	}

	err = Cfg.WriteFile(CfgFname, 0600, "")
	handle_err(err)
}
コード例 #5
0
ファイル: main.go プロジェクト: sbinet/go-github-client
func main() {
	help := flag.Bool("help", false, "Show usage")
	username := flag.String("u", "", "Specify Github user")
	password := flag.String("p", "", "Specify Github password")

	flag.Usage = func() {
		fmt.Printf("Usage:\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	if *help == true || *username == "" || *password == "" {
		flag.Usage()
		return
	}

	ghc, _ := ghclient.NewGithubClient(*username, *password, ghclient.AUTH_USER_PASSWORD)

	ghc_gists := ghgists.NewGists(ghc)

	newGist := ghgists.NewGistDataCreate()
	newGist.Description = "test go create api"
	newGist.Public = false
	newGist.Files["prova1.js"] = ghgists.GistFileContent{Content: "var x=5;"}

	res, _ := ghc_gists.CreateGist(newGist)

	jr, _ := res.Json()
	gist_id := jr.GetString("id")

	data := ghgists.NewGistDataUpdate()
	data.Files["prova1.js"] = ghgists.GistFileContent{Filename: "renamed.js", Content: "var x=5;\nvar y=10;"}

	fmt.Printf("UPDATE DATA: %v\n", data)

	res, _ = ghc_gists.UpdateGist(gist_id, data)

	jr, _ = res.Json()

	fmt.Printf("JSON: %v\n", jr)
	fmt.Printf("RAW: %v\n", res.RawHttpResponse)
}
コード例 #6
0
ファイル: main.go プロジェクト: sbinet/go-github-client
func main() {
	help := flag.Bool("help", false, "Show usage")
	username := flag.String("u", "", "Specify Github user")
	password := flag.String("p", "", "Specify Github password")

	flag.Usage = func() {
		fmt.Printf("Usage:\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	if *help == true || *username == "" || *password == "" {
		flag.Usage()
		return
	}

	ghc, err := ghclient.NewGithubClient(*username, *password, ghclient.AUTH_USER_PASSWORD)

	issuesc := ghissues.NewIssues(ghc)

	res, err := issuesc.GetRepoIssuesList("remogatto", "livegist",
		&ghissues.RepoListOptions{})

	fmt.Printf("RESPONSE: %v\nERROR: %v\n", res.RawHttpResponse, err)

	/*var body []byte = make([]byte, 10000)

	_, err = res.RawHttpResponse.Body.Read(body)*/

	//data, err := ioutil.ReadAll(res.RawHttpResponse.Body)

	jr, err := res.Json()

	/*var jr interface{}

	err := json.Unmarshal(([]byte)("[{\"prova\": 5}]"), &jr)*/

	fmt.Printf("JSON: %v\nERROR: %v\n", jr, err)
}
コード例 #7
0
ファイル: cmd_dl_ls.go プロジェクト: sbinet/goctogit
func git_run_cmd_dl_ls(cmd *commander.Command, args []string) {
	n := "github-" + cmd.Name()
	if len(args) != 1 {
		err := fmt.Errorf("%s: needs a github repository name", n)
		handle_err(err)
	}

	repo_name := args[0]
	user := cmd.Flag.Lookup("u").Value.Get().(string)
	org := cmd.Flag.Lookup("org").Value.Get().(string)

	if user == "" {
		v, err := Cfg.String("go-octogit", "username")
		handle_err(err)
		user = v
	}

	password, err := Cfg.String("go-octogit", "password")
	handle_err(err)

	ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
	handle_err(err)

	account := user
	// GET /repos/:owner/:repo/downloads
	if org != "" {
		account = org
	}
	url := path.Join("repos", account, repo_name, "downloads")

	fmt.Printf("%s: listing downloads for %s/%s...\n",
		n, account, repo_name)

	req, err := ghc.NewAPIRequest("GET", url, nil)
	handle_err(err)

	resp, err := ghc.RunRequest(req, new(http.Client))
	handle_err(err)

	if !resp.IsSuccess() {
		err = fmt.Errorf("%s: request did not succeed. got (status=%d) %v\n", n, resp.RawHttpResponse.StatusCode, resp.RawHttpResponse)
		handle_err(err)
	}

	json_arr, err := resp.JsonArray()
	if err != nil {
		handle_err(err)
	}
	for _, elmt := range json_arr {
		json := client.JsonMap(elmt.(map[string]interface{}))
		fmt.Printf("=== %s\n",
			json.GetString("name"),
		)
		fmt.Printf("%3s id=%v\n", "", int64(json.GetFloat("id")))
		fmt.Printf("%3s sz=%v bytes\n", "", int64(json.GetFloat("size")))
		descr := json.GetString("description")
		if descr != "" {
			fmt.Printf("%3s descr=%q\n", "", descr)
		}
		fmt.Printf("%3s %s\n", "", json.GetString("html_url"))
	}

	fmt.Printf("%s: listing downloads for %s/%s... [done]\n",
		n, account, repo_name)
}
コード例 #8
0
ファイル: cmd_dl_create.go プロジェクト: sbinet/goctogit
func git_run_cmd_dl_create(cmd *commander.Command, args []string) {
	n := "github-" + cmd.Name()
	if len(args) != 0 {
		err := fmt.Errorf("%s: does NOT take any positional parameter", n)
		handle_err(err)
	}

	repo_name := cmd.Flag.Lookup("repo").Value.Get().(string)
	fname := cmd.Flag.Lookup("f").Value.Get().(string)

	if repo_name == "" {
		err := fmt.Errorf("%s: needs a github repository name where to put the download", n)
		handle_err(err)
	}
	if fname == "" {
		err := fmt.Errorf("%s: needs a path to a file to upload", n)
		handle_err(err)
	}
	fname, err := filepath.Abs(fname)
	if err != nil {
		handle_err(err)
	}
	if !path_exists(fname) {
		err := fmt.Errorf("%s: needs a path to an EXISTING file to upload", n)
		handle_err(err)
	}

	fi, err := os.Lstat(fname)
	if err != nil {
		handle_err(err)
	}

	user := cmd.Flag.Lookup("u").Value.Get().(string)
	org := cmd.Flag.Lookup("org").Value.Get().(string)
	descr := cmd.Flag.Lookup("descr").Value.Get().(string)

	if user == "" {
		v, err := Cfg.String("go-octogit", "username")
		handle_err(err)
		user = v
	}

	password, err := Cfg.String("go-octogit", "password")
	handle_err(err)

	ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
	handle_err(err)

	account := user
	// POST /repos/:owner/:repo/downloads
	if org != "" {
		account = org
	}
	url := path.Join("repos", account, repo_name, "downloads")

	fmt.Printf("%s: uploading [%s] to [%s/%s]...\n",
		n, fname, account, repo_name)

	data, err := json.Marshal(
		map[string]interface{}{
			"name":        fi.Name(),
			"size":        fi.Size(),
			"description": descr,
			//"content_type": "FIXME: TODO",
		})
	handle_err(err)

	req, err := ghc.NewAPIRequest("POST", url, bytes.NewBuffer(data))
	handle_err(err)

	resp, err := ghc.RunRequest(req, new(http.Client))
	handle_err(err)

	sc := resp.RawHttpResponse.StatusCode
	switch sc {
	case 201:
		// all good
	case 422:
		// pre-existing file
		err = fmt.Errorf(
			"%s: a file with that name is already in the download area!\n",
			n)
	default:
		err = fmt.Errorf(
			"%s: request did not succeed. got (status=%d)\n%v\n",
			n, sc, resp.RawHttpResponse,
		)
	}
	if err != nil {
		handle_err(err)
	}

	s3, err := get_s3_data(resp)
	if err != nil {
		handle_err(err)
	}

	body_buf := bytes.NewBufferString("")
	body_writer := multipart.NewWriter(body_buf)
	for _, v := range [][2]string{
		{"key", s3.Path},
		{"acl", "public-read"},
		{"success_action_status", "201"},
		{"Filename", s3.Name},
		{"AWSAccessKeyId", s3.AccessKeyId},
		{"Policy", s3.Policy},
		{"Signature", s3.Signature},
		{"Content-Type", s3.ContentType},
	} {
		err = body_writer.WriteField(v[0], v[1])
		if err != nil {
			handle_err(err)
		}
	}

	file_writer, err := body_writer.CreateFormFile("file", fname)
	if err != nil {
		handle_err(err)
	}

	fh, err := os.Open(fname)
	if err != nil {
		handle_err(err)
	}
	defer fh.Close()

	_, err = io.Copy(file_writer, fh)
	if err != nil {
		handle_err(err)
	}

	content_type := body_writer.FormDataContentType()
	err = body_writer.Close()
	if err != nil {
		handle_err(err)
	}

	s3_resp, err := http.Post(s3.S3Url, content_type, body_buf)
	if err != nil {
		fmt.Printf("\n%s: s3-request failed:\n%v\n%v\n", n, err, s3_resp)
		handle_err(err)
	}

	switch s3_resp.StatusCode {
	case 201:
		// all good
	default:
		err = fmt.Errorf("%s: s3-request did not succeed. got (status=%d) %v\n", n, s3_resp.StatusCode, s3_resp)
	}
	if err != nil {
		handle_err(err)
	}

	fmt.Printf("%s: uploading [%s] to [%s/%s]... [done]\n",
		n, fname, account, repo_name)
}
コード例 #9
0
ファイル: cmd_create.go プロジェクト: sbinet/goctogit
func git_run_cmd_create(cmd *commander.Command, args []string) {
	n := "github-" + cmd.Name()
	if len(args) <= 0 {
		err := fmt.Errorf("%s: you need to give a repository name", n)
		handle_err(err)
	}

	repo_name := args[0]

	user := cmd.Flag.Lookup("u").Value.Get().(string)
	org := cmd.Flag.Lookup("org").Value.Get().(string)
	descr := cmd.Flag.Lookup("descr").Value.Get().(string)

	if user == "" {
		v, err := Cfg.String("go-octogit", "username")
		handle_err(err)
		user = v
	}

	password, err := Cfg.String("go-octogit", "password")
	handle_err(err)

	ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
	handle_err(err)

	account := user
	url := path.Join("user", "repos")
	if org != "" {
		account = org
		url = path.Join("orgs", org, "repos")
	}

	fmt.Printf("%s: creating repository [%s] with account [%s]...\n",
		n, repo_name, account)
	if descr != "" {
		fmt.Printf("%s: descr: %q\n", n, descr)
	}

	data, err := json.Marshal(
		map[string]interface{}{
			"name":          repo_name,
			"description":   descr,
			"homepage":      "",
			"private":       false,
			"has_issues":    true,
			"has_wiki":      true,
			"has_downloads": true,
		})
	handle_err(err)

	req, err := ghc.NewAPIRequest("POST", url, bytes.NewBuffer(data))
	handle_err(err)

	resp, err := ghc.RunRequest(req, new(http.Client))
	handle_err(err)

	sc := resp.RawHttpResponse.StatusCode
	if !resp.IsSuccess() && sc != 201 {
		err = fmt.Errorf("%s: request did not succeed. got (status=%d) %v\n", n, resp.RawHttpResponse.StatusCode, resp.RawHttpResponse)
		handle_err(err)
	}
	fmt.Printf("%s: you can push from an existing repository like so:\n", n)
	fmt.Printf(`
git remote add origin https://github.com/%s/%s.git
git push -u origin master

git remote add origin [email protected]:%s/%s
git push -u origin master
`, account, repo_name, account, repo_name)
	fmt.Printf("%s: creating repository [%s] with account [%s]... [done]\n",
		n, repo_name, account)
}