github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/internal/env/scm.go (about) 1 package env 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 const ( 12 defaultProvider = "github" 13 defaultAppName = "QuickFeed" 14 defaultKeyPath = "internal/config/github/quickfeed.pem" 15 defaultRepositoryPath = "$HOME/courses" 16 ) 17 18 var provider string 19 20 func init() { 21 provider = os.Getenv("QUICKFEED_SCM_PROVIDER") 22 if provider == "" { 23 provider = defaultProvider 24 } 25 } 26 27 // ScmProvider returns the current SCM provider supported by this backend. 28 func ScmProvider() string { 29 return provider 30 } 31 32 func RepositoryPath() string { 33 repositoryPath := os.Getenv("QUICKFEED_REPOSITORY_PATH") 34 if repositoryPath == "" { 35 repositoryPath = defaultRepositoryPath 36 } 37 return os.ExpandEnv(repositoryPath) 38 } 39 40 // ClientID returns the client ID for the current SCM provider. 41 func ClientID() (string, error) { 42 clientID := os.Getenv("QUICKFEED_CLIENT_ID") 43 if clientID == "" { 44 return "", fmt.Errorf("missing client ID for %s", provider) 45 } 46 return clientID, nil 47 } 48 49 // ClientSecret returns the client secret for the current SCM provider. 50 func ClientSecret() (string, error) { 51 clientSecret := os.Getenv("QUICKFEED_CLIENT_SECRET") 52 if clientSecret == "" { 53 return "", fmt.Errorf("missing client secret for %s", provider) 54 } 55 return clientSecret, nil 56 } 57 58 // AppID returns the application ID for the current SCM provider. 59 func AppID() (string, error) { 60 appID := os.Getenv("QUICKFEED_APP_ID") 61 if appID == "" { 62 return "", fmt.Errorf("missing application ID for provider %s", provider) 63 } 64 return appID, nil 65 } 66 67 // AppKey returns path to the file with .pem private key. 68 // For GitHub apps a key must be generated on the App's 69 // settings page and saved into a file. 70 func AppKey() string { 71 appKey := os.Getenv("QUICKFEED_APP_KEY") 72 if appKey == "" { 73 return filepath.Join(Root(), defaultKeyPath) 74 } 75 return appKey 76 } 77 78 // AppName returns the name of the QuickFeed app on GitHub. 79 func AppName() string { 80 appName := os.Getenv("QUICKFEED_APP_NAME") 81 if appName == "" { 82 return defaultAppName 83 } 84 return appName 85 } 86 87 func GetAccessToken() (string, error) { 88 accessToken := os.Getenv("GITHUB_ACCESS_TOKEN") 89 if len(accessToken) == 0 { 90 return "", errors.New("required 'GITHUB_ACCESS_TOKEN' is not set") 91 } 92 return accessToken, nil 93 } 94 95 // SetFakeProvider sets the provider to fake. This is only for testing. 96 // The t argument is added as a reminder that this is only for testing. 97 func SetFakeProvider(t *testing.T) { 98 t.Helper() 99 provider = "fake" 100 } 101 102 // HasAppID returns true if the environment specifies an APP_ID. 103 func HasAppID() bool { 104 _, err := AppID() 105 return err == nil 106 }