Help us shape the future of Go! Take the 2026 Go Developer Survey

githubkey

package module
v0.0.0-...-3ab796e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 6 Imported by: 0

README

CircleCI codecov Go Report Card

A library to create and delete Github SSH deploy keys.

Usage

package main

import (
	"flag"
	"log"
	"net/http"
	"os"

	"github.com/tkeech1/githubkey"
)

func main() {
	var githubUsername, githubPassword, repo, keyTitle, newKey string
	var readOnly bool
	flag.StringVar(&githubUsername, "githubUsername", "", "Github username")
	flag.StringVar(&githubPassword, "githubPassword", "", "Github password")
	flag.StringVar(&repo, "repo", "", "Github repo")
	flag.StringVar(&keyTitle, "keyTitle", "", "Github key title")
	flag.BoolVar(&readOnly, "readOnly", true, "Specifies if the Github key is read-only")
	flag.StringVar(&newKey, "newKey", "", "Github key")
	flag.Parse()

	client := &http.Client{}

    // get the key by title
	key, err := githubkey.GetDeployKey(client, githubUsername, githubPassword, repo, keyTitle)
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

    // if the key exists, delete it before creating a new key
    if key.ID >= 0 {
		err := githubkey.DeleteDeployKey(client, githubUsername, githubPassword, repo, key.ID)
		if err != nil {
			log.Fatal(err)
			os.Exit(1)
		}
	}

    // create the new key
	githubKey, err := githubkey.CreateDeployKey(client, githubUsername, githubPassword, repo, keyTitle, newKey, readOnly)
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	log.Printf("Created new key in repository: %s\n", repo)
	log.Printf("Key Name: %s\n", githubKey.Title)
	log.Printf("New Key ID: %d\n", githubKey.ID)
	log.Printf("Read-only: %t\n", githubKey.ReadOnly)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteDeployKey

func DeleteDeployKey(client Doer, githubUsername, githubPassword, repo string, keyID int64) error

DeleteDeployKey deletes a GitHub deploy key matching the keyID parameter. It returns true after a successful delete and false if the delete is unsuccessful.

Types

type CreateKeyError

type CreateKeyError struct {
	// contains filtered or unexported fields
}

CreateKeyError is an error wrapper for create operations

func (*CreateKeyError) Error

func (e *CreateKeyError) Error() string

type DeleteKeyError

type DeleteKeyError struct {
	// contains filtered or unexported fields
}

DeleteKeyError is an error wrapper for delete

func (*DeleteKeyError) Error

func (e *DeleteKeyError) Error() string

type Doer

type Doer interface {
	Do(*http.Request) (*http.Response, error)
}

Doer is an interface used to make testing easier

type GetKeyError

type GetKeyError struct {
	// contains filtered or unexported fields
}

GetKeyError is an error wrapper for get operations

func (*GetKeyError) Error

func (e *GetKeyError) Error() string

type GithubKey

type GithubKey struct {
	ID        int64  `json:"id"`
	Key       string `json:"key"`
	URL       string `json:"url"`
	Title     string `json:"title"`
	Verified  bool   `json:"verified"`
	CreatedAt string `json:"created_at"`
	ReadOnly  bool   `json:"read_only"`
}

GithubKey is the JSON representation of a Github deploy key.

func CreateDeployKey

func CreateDeployKey(client Doer, githubUsername, githubPassword, repo, keyTitle, newKey string, readOnly bool) (GithubKey, error)

CreateDeployKey creates a new GitHub deploy key.

func GetDeployKey

func GetDeployKey(client Doer, githubUsername, githubPassword, repo, keyTitle string) (GithubKey, error)

GetDeployKey returns the deploy key for a repo that matches the keyTitle parameter. It returns -1 if the key title is not found.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL