sigs.k8s.io/release-sdk@v0.11.1-0.20240417074027-8061fb5e4952/osc/config.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package osc 18 19 import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 ) 24 25 const ( 26 authFileFormat = ` 27 [general] 28 apiurl = %s 29 30 [%s] 31 user=%s 32 pass=%s 33 credentials_mgr_class=osc.credentials.PlaintextConfigFileCredentialsManager 34 ` 35 ) 36 37 // CreateOSCConfigFile creates the osc config file (~/.oscrc) that contains 38 // API URL and credentials needed to authenticate with the API 39 func CreateOSCConfigFile(apiURL, username, password string) error { 40 userHome, err := os.UserHomeDir() 41 if err != nil { 42 return fmt.Errorf("obtaining user's home directory: %w", err) 43 } 44 45 oscConfigFilePath := filepath.Join(userHome, ".oscrc") 46 authFile := fmt.Sprintf(authFileFormat, apiURL, apiURL, username, password) 47 48 if err := os.WriteFile(oscConfigFilePath, []byte(authFile), 0o600); err != nil { 49 return fmt.Errorf("writing osc config file: %w", err) 50 } 51 52 return nil 53 }