Skip to content

This project was built out of a need for a library to manage credentials files (similar to AWS credentials), their attributes, sessions, and environment variables.

License

Notifications You must be signed in to change notification settings

engi-fyi/go-credentials

Repository files navigation

go-credentials

GitHub release (latest SemVer) GitHub go.mod Go version GoDoc License
Build Quality Gate Status Coverage Go Report Card CII Best Practices

Welcome to go-credentials!

This project is being built out of a need for a library to manage credentials files (similar to AWS credentials), their attributes, sessions, and environment variables.

go-credentials logo

The Credential API is broken down into two pieces, each with their own functionality:

  1. Factory: responsible for setting variables that are global to your application, and;
    • Set alternate keys for username/password (e.g. ACCESS_TOKEN/SECRET_KEY).
    • Set the output type of the credentials (environment, ini, and json supported).
    • Responsible for logging.
  2. Credential: represents a user's credentials.
    • Username/Password defined on model.
    • Can have a profile.
    • Save and Load Credentials (and Profiles).
  3. Profile: represents a profile, containing variables specific to a profile.
    • Username/Password defined on model.
    • Set Attributes (including sections).
    • Get Attributes (including sections).

To get started, is all you need to do is create the following files.

~/.gcea/credentials

[default]
username = [email protected]
password = !my_test_pass==word

main.go

package main

import (
    "github.com/engi-fyi/go-credentials/credential"
    "github.com/engi-fyi/go-credentials/factory"
    "fmt" 
    "time"
)

func main() {
	myFact, _ := factory.New("gcea", false) // go-credentials-example-application
	myFact.ModifyLogger("trace", true) // let's see under the hood and make it pretty.
	myCredential, _ := credential.Load(myFact)

	fmt.Printf("Username: %s\n", myCredential.Username)
	fmt.Printf("Password: %s\n\n", myCredential.Password)

	myCredential.Section("metadata").SetAttribute("last_updated", time.Now().Format("02/01/2006 15:04:05"))
	myCredential.Save()
	myCredential = nil

	yourCredential, _ := credential.Load(myFact)

	fmt.Printf("Username: %s\n", yourCredential.Username)
	fmt.Printf("Password: %s\n", yourCredential.Password)

	lastUpdated := yourCredential.Section("metadata").GetAttribute("last_updated")
	fmt.Printf("Last Updated: %s\n", lastUpdated)
}