github.com/google/go-github/v49@v49.1.0/github/gitignore.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // GitignoresService provides access to the gitignore related functions in the 14 // GitHub API. 15 // 16 // GitHub API docs: https://docs.github.com/en/rest/gitignore/ 17 type GitignoresService service 18 19 // Gitignore represents a .gitignore file as returned by the GitHub API. 20 type Gitignore struct { 21 Name *string `json:"name,omitempty"` 22 Source *string `json:"source,omitempty"` 23 } 24 25 func (g Gitignore) String() string { 26 return Stringify(g) 27 } 28 29 // List all available Gitignore templates. 30 // 31 // GitHub API docs: https://docs.github.com/en/rest/gitignore/#listing-available-templates 32 func (s *GitignoresService) List(ctx context.Context) ([]string, *Response, error) { 33 req, err := s.client.NewRequest("GET", "gitignore/templates", nil) 34 if err != nil { 35 return nil, nil, err 36 } 37 38 var availableTemplates []string 39 resp, err := s.client.Do(ctx, req, &availableTemplates) 40 if err != nil { 41 return nil, resp, err 42 } 43 44 return availableTemplates, resp, nil 45 } 46 47 // Get a Gitignore by name. 48 // 49 // GitHub API docs: https://docs.github.com/en/rest/gitignore#get-a-gitignore-template 50 func (s *GitignoresService) Get(ctx context.Context, name string) (*Gitignore, *Response, error) { 51 u := fmt.Sprintf("gitignore/templates/%v", name) 52 req, err := s.client.NewRequest("GET", u, nil) 53 if err != nil { 54 return nil, nil, err 55 } 56 57 gitignore := new(Gitignore) 58 resp, err := s.client.Do(ctx, req, gitignore) 59 if err != nil { 60 return nil, resp, err 61 } 62 63 return gitignore, resp, nil 64 }