gopkg.in/openshift/source-to-image.v1@v1.2.0/pkg/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/openshift/source-to-image/pkg/api"
    11  	"github.com/openshift/source-to-image/pkg/scm/git"
    12  	utillog "github.com/openshift/source-to-image/pkg/util/log"
    13  	"github.com/spf13/cobra"
    14  	"github.com/spf13/pflag"
    15  )
    16  
    17  var log = utillog.StderrLog
    18  var savedEnvMatcher = regexp.MustCompile("env-[0-9]+")
    19  
    20  // DefaultConfigPath specifies the default location of the S2I config file
    21  const DefaultConfigPath = ".s2ifile"
    22  
    23  // Config represents a basic serialization for the S2I build options.
    24  type Config struct {
    25  	Source       string            `json:"source" yaml:"source"`
    26  	BuilderImage string            `json:"builderImage" yaml:"builderImage"`
    27  	Tag          string            `json:"tag,omitempty" yaml:"tag,omitempty"`
    28  	Flags        map[string]string `json:"flags,omitempty" yaml:"flags,omitempty"`
    29  }
    30  
    31  // Save persists the S2I command line arguments to disk.
    32  func Save(config *api.Config, cmd *cobra.Command) {
    33  	c := Config{
    34  		BuilderImage: config.BuilderImage,
    35  		Source:       config.Source.String(),
    36  		Tag:          config.Tag,
    37  		Flags:        make(map[string]string),
    38  	}
    39  	cmd.Flags().Visit(func(f *pflag.Flag) {
    40  		if f.Name == "env" {
    41  			for i, env := range config.Environment {
    42  				c.Flags[fmt.Sprintf("%s-%d", f.Name, i)] = fmt.Sprintf("%s=%s", env.Name, env.Value)
    43  			}
    44  		} else {
    45  			c.Flags[f.Name] = f.Value.String()
    46  		}
    47  	})
    48  	data, err := json.Marshal(c)
    49  	if err != nil {
    50  		log.V(1).Infof("Unable to serialize to %s: %v", DefaultConfigPath, err)
    51  		return
    52  	}
    53  	if err := ioutil.WriteFile(DefaultConfigPath, data, 0644); err != nil {
    54  		log.V(1).Infof("Unable to save %s: %v", DefaultConfigPath, err)
    55  	}
    56  	return
    57  }
    58  
    59  // Restore loads the arguments from disk and prefills the Request
    60  func Restore(config *api.Config, cmd *cobra.Command) {
    61  	data, err := ioutil.ReadFile(DefaultConfigPath)
    62  	if err != nil {
    63  		data, err = ioutil.ReadFile(".stifile")
    64  		if err != nil {
    65  			log.V(1).Infof("Unable to restore %s: %v", DefaultConfigPath, err)
    66  			return
    67  		}
    68  		log.Infof("DEPRECATED: Use %s instead of .stifile", DefaultConfigPath)
    69  	}
    70  
    71  	c := Config{}
    72  	if err := json.Unmarshal(data, &c); err != nil {
    73  		log.V(1).Infof("Unable to parse %s: %v", DefaultConfigPath, err)
    74  		return
    75  	}
    76  
    77  	source, err := git.Parse(c.Source)
    78  	if err != nil {
    79  		log.V(1).Infof("Unable to parse %s: %v", c.Source, err)
    80  		return
    81  	}
    82  
    83  	config.BuilderImage = c.BuilderImage
    84  	config.Source = source
    85  	config.Tag = c.Tag
    86  
    87  	envOverride := false
    88  	if cmd.Flag("env").Changed {
    89  		envOverride = true
    90  	}
    91  
    92  	for name, value := range c.Flags {
    93  		// Do not change flags that user sets. Allow overriding of stored flags.
    94  		if name == "env" {
    95  			if envOverride {
    96  				continue
    97  			}
    98  			for _, v := range strings.Split(value, ",") {
    99  				cmd.Flags().Set(name, v)
   100  			}
   101  		} else if savedEnvMatcher.MatchString(name) {
   102  			if envOverride {
   103  				continue
   104  			}
   105  			cmd.Flags().Set("env", value)
   106  		} else {
   107  			if cmd.Flag(name).Changed {
   108  				continue
   109  			}
   110  			cmd.Flags().Set(name, value)
   111  		}
   112  	}
   113  }