Ejemplo n.º 1
0
func gistFiles() map[github.GistFilename]github.GistFile {
	files := map[github.GistFilename]github.GistFile{}
	args := flag.Args()
	if paste {
		name := "-"
		content, err := pasteFromClipboard()
		if err != nil {
			log.Fatal(err)
		}
		file := github.GistFile{Filename: &name, Content: &content}
		files[github.GistFilename(name)] = file
	} else if len(args) == 0 {
		name := "-"
		bytes, _ := ioutil.ReadAll(os.Stdin)
		content := string(bytes)
		file := github.GistFile{Filename: &name, Content: &content}
		files[github.GistFilename(name)] = file
	} else {
		for _, arg := range args {
			bytes, err := ioutil.ReadFile(arg)
			if err != nil {
				log.Fatal(err)
			}
			content := string(bytes)
			basename := path.Base(arg)
			file := github.GistFile{Filename: &basename, Content: &content}
			files[github.GistFilename(arg)] = file
		}
	}

	return files
}
Ejemplo n.º 2
0
func makeGist(httpBody io.Reader) (github.Gist, error) {
	// get body
	byteBody, err := ioutil.ReadAll(httpBody)
	if err != nil {
		return github.Gist{}, fmt.Errorf("get body error: %v", err)
	}

	// unmarshal
	postGist := struct {
		FileName    string `json:"file_name"`
		Description string `json:"description"`
		Public      bool   `json:"public"`
		Code        string `json:"code"`
	}{}
	err = json.Unmarshal(byteBody, &postGist)
	if err != nil {
		return github.Gist{}, fmt.Errorf("json unmarshal error: %v", err)
	}

	// create gist
	gist := github.Gist{
		Description: github.String(strings.TrimSpace(postGist.Description)),
		Public:      &postGist.Public,
		Files: map[github.GistFilename]github.GistFile{
			github.GistFilename(strings.TrimSpace(postGist.FileName) + ".go"): github.GistFile{
				Content: &postGist.Code,
			},
		},
	}

	return gist, nil
}
Ejemplo n.º 3
0
func create(desc string, pub bool, files map[string]string) (*github.Gist, error) {

	ghat := getAccessToken()

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: ghat},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := github.NewClient(tc)

	f := make(map[github.GistFilename]github.GistFile)

	for k := range files {
		_k := github.GistFilename(k)
		f[_k] = github.GistFile{Content: github.String(files[k])}
	}

	gist := &github.Gist{
		Description: github.String(desc),
		Public:      github.Bool(pub),
		Files:       f,
	}

	gist, _, err := client.Gists.Create(gist)

	return gist, err
}
Ejemplo n.º 4
0
// Push exports the local encrypted trousseau data store to
// a Github private gist
func (gs *GistStorage) Push(localPath, remotePath string) (err error) {
	var public bool = false

	// Read the encrypted data store content
	fileBytes, err := ioutil.ReadFile(localPath)
	if err != nil {
		return err
	}
	fileContent := string(fileBytes)

	// Build a Gist file store
	files := map[github.GistFilename]github.GistFile{
		github.GistFilename(remotePath): github.GistFile{
			Content: &fileContent,
		},
	}

	// Create a gist representation ready to be
	// pushed over network
	gist := &github.Gist{
		Public: &public,
		Files:  files,
	}

	// Push the gist to github
	_, _, err = gs.connexion.Gists.Create(gist)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 5
0
func (s *Server) GroupExportGistHandler(w http.ResponseWriter, r *http.Request) {
	id, err := getIDFromMux(mux.Vars(r))
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		writeJSON(w, err)
		return
	}

	s.Lock()
	defer s.Unlock()
	p, err := s.Export(id)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		writeJSON(w, Error{err.Error()})
		return
	}

	client := s.newGithubClient()

	contentBytes, err := json.Marshal(p)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		writeJSON(w, Error{err.Error()})
		return
	}

	fname := strings.Replace(p.Label, " ", "_", -1) + ".json"

	public := true
	content := string(contentBytes)

	g := github.Gist{
		Description: &p.Label,
		Public:      &public,
		Files: map[github.GistFilename]github.GistFile{
			github.GistFilename(fname): github.GistFile{
				Content: &content,
			},
		},
	}

	gist, _, err := client.Gists.Create(&g)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		writeJSON(w, Error{err.Error()})
		return
	}

	gistBytes, err := json.Marshal(gist)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		writeJSON(w, Error{err.Error()})
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.Write(gistBytes)
}
Ejemplo n.º 6
0
func main() {
	flag.Parse()

	var stdin []byte
	if !isTerminal(0) {
		stdin, _ = ioutil.ReadAll(os.Stdin)
	}
	if len(stdin) == 0 && flag.NArg() == 0 {
		log.Fatalln("No stdin or files provided")
	}

	gist := &github.Gist{Public: public, Files: make(map[github.GistFilename]github.GistFile)}
	if *description != "" {
		gist.Description = description
	}

	if len(stdin) > 0 {
		content := string(stdin)
		gist.Files[github.GistFilename(*name)] = github.GistFile{Content: &content}
	}

	for _, file := range flag.Args() {
		data, err := ioutil.ReadFile(file)
		if err != nil {
			log.Fatalf("Error reading %s: %s", file, err)
		}
		content := string(data)
		gist.Files[github.GistFilename(filepath.Base(file))] = github.GistFile{Content: &content}
	}

	client := github.NewClient(nil)
	if *token != "" {
		t := &oauth.Transport{Token: &oauth.Token{AccessToken: *token}}
		client = github.NewClient(t.Client())
	}

	res, _, err := client.Gists.Create(gist)
	if err != nil {
		log.Fatalln("Unable to create gist:", err)
	}

	fmt.Println(*res.HTMLURL)
}
Ejemplo n.º 7
0
func create(filename string, public bool) {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token()},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := github.NewClient(tc)

	var r io.Reader
	if filename == "-" {
		r = os.Stdin
	} else {
		f, err := os.Open(filename)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		r = f
	}

	content, err := ioutil.ReadAll(r)
	if err != nil {
		log.Fatal(err)
	}
	var strBody = string(content)

	gistFilename := filepath.Base(filename)
	if filename == "-" {
		h := sha1.New()
		h.Write(content)
		h.Sum(nil)
		gistFilename = fmt.Sprintf("%x", sha1.Sum(nil))
	}

	g := github.Gist{
		Files: map[github.GistFilename]github.GistFile{
			github.GistFilename(gistFilename): github.GistFile{
				Content: &strBody,
			},
		},
		Public: &public,
	}

	created, _, err := client.Gists.Create(&g)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Gist: ", *created.HTMLURL)
}
Ejemplo n.º 8
0
func fetchFile(client *github.Client, id, sha, filename string) (*github.Gist, *File, error) {
	var gist *github.Gist
	var err error
	if sha != "" {
		gist, _, err = client.Gists.GetRevision(id, sha)
	} else {
		gist, _, err = client.Gists.Get(id)
	}
	if err != nil {
		return gist, nil, err
	}
	if content := gist.Files[github.GistFilename(filename)].Content; content != nil {
		return gist, &File{
			Name:    filename,
			Content: *content,
		}, nil
	}
	return gist, nil, ErrNotFound
}
Ejemplo n.º 9
0
func CreateGist(cli *CLI, token gha.RoundTripper) error {
	fname := github.GistFilename(cli.FileName)
	files := make(map[github.GistFilename]github.GistFile)
	files[fname] = github.GistFile{Content: &cli.FileContent}

	c := github.NewClient(&http.Client{
		Transport: token,
	})

	gist, _, err := c.Gists.Create(&github.Gist{
		Files:  files,
		Public: &cli.Public,
	})
	if err != nil {
		return err
	}

	fmt.Println(*gist.HTMLURL)
	return nil
}
Ejemplo n.º 10
0
func updateInput(client *github.Client, gist *github.Gist, inputs ...*Input) {
	for _, input := range inputs {
		log.Info("Input: %+v\n", input)
		if input == nil {
			continue
		}
		for i, file := range input.Files {
			log.Info("%+v", file)
			if file.Content == "" {
				if gistId, content := file.Id, gist.Files[github.GistFilename(file.Name)].Content; gistId == "" && content != nil {
					input.Files[i].Content = *content
					continue
				}
				if _, fetchedFile, err := fetchFile(client, file.Id, file.Sha, file.Name); err == nil {
					log.Info("Fetching %s from sha %s", file.Name, file.Id)
					input.Files[i].Content = fetchedFile.Content
				}
			}
		}
		//log.Info("Input: %+v\n", input)
	}
}
Ejemplo n.º 11
0
// Pull imports the encrypted trousseau data store from
// a Github gist
func (gs *GistStorage) Pull(remotePath, localPath string) (err error) {
	var gist *github.Gist

	// Fetch the user's gists list
	gists, _, err := gs.connexion.Gists.List(gs.User, nil)
	if err != nil {
		return err
	}

	// Find a gist containing trousseau data store
	for _, g := range gists {
		for k, _ := range g.Files {
			if string(k) == remotePath {
				gist = &g
				break
			}
		}

		if gist != nil {
			break
		}
	}

	// Download the gist file content
	gist, _, err = gs.connexion.Gists.Get(*gist.ID)

	// Write the downloaded file content to the local trousseau
	// data store file
	fileContent := []byte(*gist.Files[github.GistFilename(remotePath)].Content)
	err = ioutil.WriteFile(localPath, fileContent, 0600)
	if err != nil {
		return err
	}

	return nil
}