gitinfo

package module
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 11 Imported by: 1

README

gitinfo

Get git repo metagata (lib) and generate gitinfo.json via go generate (cmd)

GoDoc codecov Build Status GoCard GitHub Release GitHub license

This package uses external git binary for creating a file with git metagata like

{
  "version": "v0.12.0-1-g99a5776",
  "repository": "git@github.com:pgmig/gitinfo.git",
  "modified": "2021-02-08T23:00:47+03:00"
}

This file (named `gitinfo.json by default) used later for

  • embedding with filesystems
  • showing project metagata

Install

go get github.com/pgmig/gitinfo/...

Usage

Create gitinfo.json

Run go:generate just before embedding:

// Generate gitinfo.json
//go:generate gitinfo ../../html

// Generate resource.go by [parcello](github.com/phogolabs/parcello)
//go:generate parcello -q -r -d ../../html
Read gitinfo.json

Read metadata from .gitinfo.json if it exists, fetch from git otherwise

var gi gitinfo.GitInfo
err = gitinfo.New(log, cfg).Make("cmd/", &gi)
Generate gitinfo.json for single dir
//go:generate gitinfo dir
Generate gitinfo.json files for dir/*/ dirs

Used when dir contains git submodules

//go:generate gitinfo dir/*

License

The MIT License (MIT), see LICENSE.

Copyright (c) 2019-2021 Aleksey Kovrizhkin lekovr+pgmig@gmail.com

Documentation

Overview

Package gitinfo used for generate and read gitinfo.json file which contains git repository data like this:

{
  "version": "v0.33-1-g4f4575a",
  "repository": "https://github.com/pgmig-sql/pgmig.git",
  "modified": "2019-12-24T02:44:51+03:00"
}

This file is intended to be included in embedded FS

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrPathMustNotBeEmpty raised if given path is empty
	ErrPathMustNotBeEmpty = errors.New("Path must not be empty")
	// ErrPathMustBeDir raised if given path is not directory
	ErrPathMustBeDir = errors.New("Path must be a directory")
)

Functions

func MkTime

func MkTime(in []byte, rv *time.Time) error

MkTime converts to time.Time result of `git show -s --format=format:%ct HEAD`

Types

type Config

type Config struct {
	Debug  bool   `long:"debug" description:"Show debug data"`
	File   string `long:"file" default:"gitinfo.json" description:"GitInfo json filename"`
	GitBin string `long:"gitbin" default:"git" description:"Git binary name"`
	// Root may be hardcoded if app uses embedded FS, so do not showed in help
	Root string
}

Config holds all config vars

type File added in v0.11.3

type File interface {
	io.Closer
	io.Reader
}

File is an interface for FileSystem.Open func

type FileSystem added in v0.11.3

type FileSystem interface {
	Open(name string) (File, error)
}

FileSystem holds all of used filesystem access methods

type GitInfo

type GitInfo struct {
	Version    string    `json:"version"`
	Repository string    `json:"repository"`
	Modified   time.Time `json:"modified"`
}

GitInfo holds git repository metadata

type Service

type Service struct {
	Config Config
	Log    logr.Logger
	GI     *GitInfo
	// contains filtered or unexported fields
}

Service holds service data

func New

func New(log logr.Logger, cfg Config) *Service

New returns service object with config

Example
log := genericr.New(func(e genericr.Entry) {
	fmt.Fprintln(os.Stderr, e.String())
})
cfg := gitinfo.Config{}
// Fill config with default values
p := flags.NewParser(&cfg, flags.Default|flags.IgnoreUnknown)
_, err := p.Parse()
if err != nil {
	log.Error(err, "Config")
	os.Exit(1)
}

var gi gitinfo.GitInfo
err = gitinfo.New(log, cfg).Make("cmd/", &gi)
if err != nil {
	fmt.Printf("%#v\n", err)
}
fmt.Printf("%v\n%v\n%v\n",
	gi.Modified != time.Time{},
	gi.Version != "",
	strings.HasSuffix(gi.Repository, "pgmig/gitinfo.git"),
)
Output:

true
true
true

func (Service) Make

func (srv Service) Make(path string, gi *GitInfo) error

Make prepares GitInfo data

Example
package main

import (
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/wojas/genericr"

	"github.com/pgmig/gitinfo"
)

func main() {
	log := genericr.New(func(e genericr.Entry) {
		fmt.Fprintln(os.Stderr, e.String())
	})
	gi := gitinfo.GitInfo{}
	err := gitinfo.New(log, gitinfo.Config{GitBin: "git"}).Make("cmd/", &gi)
	if err != nil {
		fmt.Printf("%#v\n", err)
	}
	fmt.Printf("%v\n%v\n%v\n",
		gi.Modified != time.Time{},
		gi.Version != "",
		strings.HasSuffix(gi.Repository, "pgmig/gitinfo.git"),
	)
}
Output:

true
true
true

func (Service) Modified added in v0.12.1

func (srv Service) Modified(path string, rv *time.Time) error

Modified fills rv with package last commit timestamp

func (Service) Read

func (srv Service) Read(fs FileSystem, path string) (*GitInfo, error)

Read reads GitInfo data from file

func (Service) ReadOrMake added in v0.11.3

func (srv Service) ReadOrMake(fs FileSystem, path string) (*GitInfo, error)

ReadOrMake reads GitInfo data from file or makes it from git on the fly

func (Service) Repository added in v0.12.1

func (srv Service) Repository(path string, rv *string) error

Repository fills rv with package repo from git

func (Service) Version added in v0.12.1

func (srv Service) Version(path string, rv *string) error

Version fills rv with package version from git

func (Service) Write

func (srv Service) Write(path string, gi *GitInfo) error

Write saves GitInfo data to file, prepare it if none given

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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