github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/spec/commit_meta.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package spec
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"strings"
    28  	"time"
    29  
    30  	flag "github.com/juju/gnuflag"
    31  
    32  	"github.com/dolthub/dolt/go/store/datas"
    33  	"github.com/dolthub/dolt/go/store/types"
    34  )
    35  
    36  const CommitMetaDateFormat = time.RFC3339
    37  
    38  var (
    39  	commitMetaDate            string
    40  	commitMetaMessage         string
    41  	commitMetaKeyValueStrings string
    42  	commitMetaKeyValuePaths   string
    43  )
    44  
    45  // RegisterCommitMetaFlags registers command line flags used for creating commit meta structs.
    46  func RegisterCommitMetaFlags(flags *flag.FlagSet) {
    47  	flags.StringVar(&commitMetaDate, "date", "", "alias for -meta 'date=<date>'. '<date>' must be iso8601-formatted. If '<date>' is empty, it defaults to the current date.")
    48  	flags.StringVar(&commitMetaMessage, "message", "", "alias for -meta 'message=<message>'")
    49  	flags.StringVar(&commitMetaKeyValueStrings, "meta", "", "'<key>=<value>' - creates a metadata field called 'key' set to 'value'. Value should be human-readable encoded.")
    50  	flags.StringVar(&commitMetaKeyValuePaths, "meta-p", "", "'<key>=<path>' - creates a metadata field called 'key' set to the value at <path>")
    51  }
    52  
    53  // CreateCommitMetaStruct creates and returns a Noms struct suitable for use in CommitOptions.Meta.
    54  // It returns types.EmptyStruct and an error if any issues are encountered.
    55  // Database is used only if commitMetaKeyValuePaths are provided on the command line and values need to be resolved.
    56  // Date should be ISO 8601 format (see CommitMetaDateFormat), if empty the current date is used.
    57  // The values passed as command line arguments (if any) are merged with the values provided as function arguments.
    58  func CreateCommitMetaStruct(ctx context.Context, db datas.Database, date, message string, keyValueStrings map[string]string, keyValuePaths map[string]types.Value) (types.Struct, error) {
    59  	metaValues := types.StructData{}
    60  
    61  	resolvePathFunc := func(path string) (types.Value, error) {
    62  		absPath, err := NewAbsolutePath(path)
    63  		if err != nil {
    64  			return nil, fmt.Errorf("bad path for meta-p: %s", path)
    65  		}
    66  		return absPath.Resolve(ctx, db), nil
    67  	}
    68  	parseMetaStrings := func(param string, resolveAsPaths bool) error {
    69  		if param == "" {
    70  			return nil
    71  		}
    72  		ms := strings.Split(param, ",")
    73  		for _, m := range ms {
    74  			kv := strings.Split(m, "=")
    75  			if len(kv) != 2 {
    76  				return fmt.Errorf("unable to parse meta value: %s", m)
    77  			}
    78  			if !types.IsValidStructFieldName(kv[0]) {
    79  				return fmt.Errorf("invalid meta key: %s", kv[0])
    80  			}
    81  			if resolveAsPaths {
    82  				v, err := resolvePathFunc(kv[1])
    83  				if err != nil {
    84  					return err
    85  				}
    86  				metaValues[kv[0]] = v
    87  			} else {
    88  				metaValues[kv[0]] = types.String(kv[1])
    89  			}
    90  		}
    91  		return nil
    92  	}
    93  
    94  	if err := parseMetaStrings(commitMetaKeyValueStrings, false); err != nil {
    95  		return types.EmptyStruct(db.Format()), err
    96  	}
    97  	if err := parseMetaStrings(commitMetaKeyValuePaths, true); err != nil {
    98  		return types.EmptyStruct(db.Format()), err
    99  	}
   100  
   101  	for k, v := range keyValueStrings {
   102  		if !types.IsValidStructFieldName(k) {
   103  			return types.EmptyStruct(db.Format()), fmt.Errorf("invalid meta key: %s", k)
   104  		}
   105  		metaValues[k] = types.String(v)
   106  	}
   107  	for k, v := range keyValuePaths {
   108  		if !types.IsValidStructFieldName(k) {
   109  			return types.EmptyStruct(db.Format()), fmt.Errorf("invalid meta key: %s", k)
   110  		}
   111  		metaValues[k] = v
   112  	}
   113  
   114  	if date == "" {
   115  		date = commitMetaDate
   116  	}
   117  	if date == "" {
   118  		date = time.Now().UTC().Format(CommitMetaDateFormat)
   119  	} else {
   120  		_, err := time.Parse(CommitMetaDateFormat, date)
   121  		if err != nil {
   122  			return types.EmptyStruct(db.Format()), fmt.Errorf("unable to parse date: %s, error: %s", date, err)
   123  		}
   124  	}
   125  	metaValues["date"] = types.String(date)
   126  
   127  	if message != "" {
   128  		metaValues["message"] = types.String(message)
   129  	} else if commitMetaMessage != "" {
   130  		metaValues["message"] = types.String(commitMetaMessage)
   131  	}
   132  	return types.NewStruct(db.Format(), "Meta", metaValues)
   133  }