1// Copyright 2020 Google LLC 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// https://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 client implements the hacksaw cli client 16package client 17 18import ( 19 "io/ioutil" 20 "os" 21 "path" 22 "reflect" 23 24 "android.googlesource.com/platform/tools/treble.git/hacksaw/bind" 25 "android.googlesource.com/platform/tools/treble.git/hacksaw/config" 26) 27 28func ensureConfigFileExists(cfgPath string) error { 29 _, err := os.Stat(cfgPath) 30 if os.IsNotExist(err) { 31 //continue to init config file 32 } else { 33 return err 34 } 35 36 //init config file 37 if err = os.MkdirAll(path.Dir(cfgPath), os.ModePerm); err != nil { 38 return err 39 } 40 return ioutil.WriteFile(cfgPath, []byte("{}"), os.ModePerm) 41} 42 43func HandleCommand(workspaceTopDir string, pathBinder bind.PathBinder, args []string) error { 44 cfgPath := path.Join(workspaceTopDir, "config.json") 45 if err := ensureConfigFileExists(cfgPath); err != nil { 46 return err 47 } 48 cfg := config.GetConfig() 49 if err := cfg.ReadConfigFromFile(cfgPath); err != nil { 50 return err 51 } 52 //Save a copy of the config to detect changes 53 savedCfg := cfg.Copy() 54 cmd := NewCommand(pathBinder, workspaceTopDir) 55 if err := cmd.Handle(args); err != nil { 56 return err 57 } 58 if reflect.DeepEqual(savedCfg, cfg) { 59 return nil 60 } 61 return cfg.WriteConfigToFile(cfgPath) 62} 63