github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/config/config.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  // Package config contains types and functions for dealing with
    16  // git-dolt configuration, including config file I/O.
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"strings"
    23  
    24  	"github.com/dolthub/dolt/go/cmd/git-dolt/env"
    25  	"github.com/dolthub/dolt/go/cmd/git-dolt/utils"
    26  )
    27  
    28  // GitDoltConfig represents the configuration for a git-dolt integration.
    29  type GitDoltConfig struct {
    30  	// Version is the version of the git-dolt protocol being used.
    31  	Version string
    32  	// Remote is the url of the dolt remote.
    33  	Remote string
    34  	// Revision is the revision of the remote that this git-dolt pointer links to.
    35  	Revision string
    36  }
    37  
    38  // Parse parses a git-dolt config string into a struct.
    39  func Parse(c string) (GitDoltConfig, error) {
    40  	lines := strings.Split(c, "\n")
    41  	config := make(map[string]string)
    42  
    43  	for _, line := range lines {
    44  		setting := strings.Split(line, " ")
    45  		if len(setting) == 2 {
    46  			config[setting[0]] = setting[1]
    47  		}
    48  	}
    49  
    50  	// default to the current version of git-dolt
    51  	if config["version"] == "" {
    52  		config["version"] = env.Version
    53  	}
    54  
    55  	requiredProps := []string{"remote", "revision"}
    56  
    57  	for _, prop := range requiredProps {
    58  		if _, ok := config[prop]; !ok {
    59  			return GitDoltConfig{}, fmt.Errorf("no %s specified", prop)
    60  		}
    61  	}
    62  
    63  	return GitDoltConfig{
    64  		Version:  config["version"],
    65  		Remote:   config["remote"],
    66  		Revision: config["revision"],
    67  	}, nil
    68  }
    69  
    70  // Load loads a GitDoltConfig from the pointer file with the given filename.
    71  func Load(ptrFname string) (GitDoltConfig, error) {
    72  	ptrFname = utils.EnsureSuffix(ptrFname, ".git-dolt")
    73  	ptrData, err := ioutil.ReadFile(ptrFname)
    74  	if err != nil {
    75  		return GitDoltConfig{}, fmt.Errorf("can't find pointer file %s", ptrFname)
    76  	}
    77  
    78  	config, err := Parse(string(ptrData))
    79  	if err != nil {
    80  		return GitDoltConfig{}, fmt.Errorf("error parsing config file: %v", err)
    81  	}
    82  
    83  	return config, nil
    84  }
    85  
    86  // Write writes to the pointer file with the given filename,
    87  // creating or overwriting it with the given contents.
    88  func Write(ptrFname string, ptrContents string) error {
    89  	ptrFname = utils.EnsureSuffix(ptrFname, ".git-dolt")
    90  	if err := ioutil.WriteFile(ptrFname, []byte(ptrContents), 0644); err != nil {
    91  		return fmt.Errorf("error writing git-dolt pointer file at %s: %v", ptrFname, err)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func (c GitDoltConfig) String() string {
    98  	return fmt.Sprintf("version %s\nremote %s\nrevision %s\n", c.Version, c.Remote, c.Revision)
    99  }