gogitconfig

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: GPL-2.0 Imports: 6 Imported by: 1

README

Git Config

This is a tool to extract git configurations in go.

It reads the data directly from git config files and returns the values. It works using key value pair, if the key isn't set, then it returns an Error String. The value of the key is set to empty and an error message set accordingly.

Only use the values if the value of error code is nil.

The configurations are returned in order of their precedence, the first one to be found is returned.

Look at the git-config FILES section about precidence

Usage

You should pass in the key of the git value you want to obtain. This follows the git config model:

[user]
    name = Your Name
    email = your@email.com
; Others

To get the username, you should pass in "user.name" and so on. The starting point (where you'd like the tool to start git config lookup) can be left blank if the starting point is the current directory or you don't require local config.

Example:

import "github.com/gekkowrld/go-gitconfig"

// The struct to be passed.
// The required field is ConfigKey
// all the others (well 2) are optional
myConfig := OptionsPassed{
    LookupStartLocation: "/path/to/repo", // Adjust this path as needed
    ConfigLevel:         0,
    ConfigKey:           "user.name",
}

// Call the function with the test case
username, err := gogitconfig.GetValue(myConfig)

fmt.Printf("Your git username is %s\n", username)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValue

func GetValue(optsPassed OptionsPassed) (string, error)

GetValue returns the value of the git keys as specified by git config files. If the key is not set, an error is returned with an empty string The startingPoint is optional and should only be set if there is need for constant starting point. The value is searched from the local configuration file upwards. No error is returned if any of the configuration files don't exist or have an error. An error is only returned when the key is not found.

Types

type OptionsPassed added in v0.0.3

type OptionsPassed struct {
	LookupStartLocation string
	ConfigLevel         int
	ConfigKey           string
}

Options Passed Set:

  • the startiing location
  • the config key e.g user.key
  • the config level

The config level is one of this values:

  • 1 -> "local"
  • 2 -> "global"
  • 3 -> "system"
  • not set -> whichever comes first
  • 0 and any other number -> whichever comes first

Notes

Bugs

  • Can't handle windows paths yet.

  • Can't get system configuration, I have to figure out how to get $(prefix)

Jump to

Keyboard shortcuts

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