github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/dao/configure.go (about)

     1  /*
     2  Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     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  
    16  package dao
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/awslabs/clencli/cobra/aid"
    23  	"github.com/awslabs/clencli/cobra/model"
    24  )
    25  
    26  // GetConfigurations read the current configurations file and return its model
    27  func GetConfigurations() (model.Configurations, error) {
    28  	var confs model.Configurations
    29  	v, err := aid.ReadConfig(aid.GetAppInfo().ConfigurationsName)
    30  	if err != nil {
    31  		return confs, fmt.Errorf("unable to read configurations\n%v", err)
    32  	}
    33  
    34  	err = v.Unmarshal(&confs)
    35  	if err != nil {
    36  		return confs, fmt.Errorf("unable to unmarshall configurations\n%v", err)
    37  	}
    38  
    39  	return confs, err
    40  }
    41  
    42  // GetReadMe TODO...
    43  func GetReadMe() (model.ReadMe, error) {
    44  	var readMe model.ReadMe
    45  	wd, err := os.Getwd()
    46  	if err != nil {
    47  		return readMe, fmt.Errorf("unable to get the current working directory:\n%v", err)
    48  	}
    49  
    50  	configPath := wd + "/clencli"
    51  	configName := "readme"
    52  	configType := "yaml"
    53  
    54  	v, err := aid.ReadConfigAsViper(configPath, configName, configType)
    55  	if err != nil {
    56  		return readMe, fmt.Errorf("unable to read configurations\n%v", err)
    57  	}
    58  
    59  	err = v.Unmarshal(&readMe)
    60  	if err != nil {
    61  		return readMe, fmt.Errorf("unable to unmarshall configurations\n%v", err)
    62  	}
    63  
    64  	return readMe, err
    65  }
    66  
    67  // GetConfigurationProfile returns credentials of a profile
    68  func GetConfigurationProfile(name string) (model.ConfigurationProfile, error) {
    69  	configurations, err := GetConfigurations()
    70  
    71  	if err != nil {
    72  		return (model.ConfigurationProfile{}), err
    73  	}
    74  
    75  	for _, profile := range configurations.Profiles {
    76  		if profile.Name == name {
    77  			return profile, err
    78  		}
    79  	}
    80  
    81  	return (model.ConfigurationProfile{}), err
    82  }
    83  
    84  // GetUnsplashRandomPhotoParameters TODO ...
    85  func GetUnsplashRandomPhotoParameters(name string) model.UnsplashRandomPhotoParameters {
    86  	profile, _ := GetConfigurationProfile(name)
    87  
    88  	if len(profile.Configurations) > 0 {
    89  		// TODO: improve this logic
    90  		for _, conf := range profile.Configurations {
    91  			return conf.Unsplash.RandomPhoto.Parameters
    92  		}
    93  	}
    94  
    95  	return (model.UnsplashRandomPhotoParameters{})
    96  }
    97  
    98  // GetCredentials read the current credentials file and return its model
    99  func GetCredentials() (model.Credentials, error) {
   100  	var creds model.Credentials
   101  	v, err := aid.ReadConfig(aid.GetAppInfo().CredentialsName)
   102  	if err != nil {
   103  		return creds, fmt.Errorf("unable to read credentials\n%v", err)
   104  	}
   105  
   106  	err = v.Unmarshal(&creds)
   107  	if err != nil {
   108  		return creds, fmt.Errorf("unable to unmarshall credentials\n%v", err)
   109  	}
   110  
   111  	return creds, err
   112  }
   113  
   114  // GetCredentialProfile returns credentials of a profile
   115  func GetCredentialProfile(name string) (model.CredentialProfile, error) {
   116  	credentials, err := GetCredentials()
   117  
   118  	if err != nil {
   119  		return (model.CredentialProfile{}), err
   120  	}
   121  
   122  	for _, profile := range credentials.Profiles {
   123  		if profile.Name == name {
   124  			return profile, err
   125  		}
   126  	}
   127  
   128  	return (model.CredentialProfile{}), err
   129  }
   130  
   131  // GetCredentialByProvider return credentials based on the given provider, if non-existent, return an empty credential
   132  func GetCredentialByProvider(profile string, provider string) (model.Credential, error) {
   133  	cp, err := GetCredentialProfile(profile)
   134  	if err != nil {
   135  		return (model.Credential{}), err
   136  	}
   137  
   138  	for _, c := range cp.Credentials {
   139  		if c.Provider == provider {
   140  			return c, err
   141  		}
   142  	}
   143  
   144  	return (model.Credential{}), err
   145  }
   146  
   147  // SaveConfigurations saves the given configuration onto the configurations file
   148  func SaveConfigurations(configurations model.Configurations) error {
   149  	return aid.WriteInterfaceToFile(configurations, aid.GetAppInfo().ConfigurationsPath)
   150  }
   151  
   152  // SaveCredentials saves the given credential onto the credentials file
   153  func SaveCredentials(credentials model.Credentials) error {
   154  	return aid.WriteInterfaceToFile(credentials, aid.GetAppInfo().CredentialsPath)
   155  }