github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/aid/root.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 aid
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/awslabs/clencli/cobra/model"
    23  	"github.com/sirupsen/logrus"
    24  )
    25  
    26  // GetAppInfo return information about clencli settings
    27  func GetAppInfo() model.App {
    28  	var err error
    29  
    30  	configDir, err := os.UserConfigDir()
    31  	if err != nil {
    32  		logrus.Fatalln("unable to read user configuration directory\n%v", err)
    33  	}
    34  
    35  	sep := string(os.PathSeparator)
    36  
    37  	var app model.App
    38  	app.Name = "clencli"
    39  	app.ConfigurationsDir = configDir + sep + app.Name
    40  	app.ConfigurationsName = "configurations"
    41  	app.ConfigurationsType = "yaml"
    42  	app.ConfigurationsPath = app.ConfigurationsDir + sep + app.ConfigurationsName + "." + app.ConfigurationsType
    43  	app.ConfigurationsPermissions = os.ModePerm
    44  	app.CredentialsName = "credentials"
    45  	app.CredentialsType = "yaml"
    46  	app.CredentialsPath = app.ConfigurationsDir + sep + app.CredentialsName + "." + app.CredentialsType
    47  	app.CredentialsPermissions = os.ModePerm
    48  	app.LogsDir = app.ConfigurationsDir
    49  	app.LogsName = "logs"
    50  	app.LogsType = "json"
    51  	app.LogsPath = app.LogsDir + sep + app.LogsName + "." + app.LogsType
    52  	app.LogsPermissions = os.ModePerm
    53  
    54  	app.WorkingDir, err = os.Getwd()
    55  	if err != nil {
    56  		fmt.Printf("Unable to detect the current directory\n%v", err)
    57  		os.Exit(1)
    58  	}
    59  
    60  	return app
    61  }
    62  
    63  // SetupLoggingOutput set logrun output file
    64  func SetupLoggingOutput(path string) error {
    65  	if path == "" {
    66  		app := GetAppInfo()
    67  		path = app.LogsPath
    68  	}
    69  
    70  	file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
    71  	if err != nil {
    72  		return fmt.Errorf("unable to open log file\n%v", err)
    73  	}
    74  
    75  	logrus.SetFormatter(&logrus.JSONFormatter{})
    76  	logrus.SetOutput(file)
    77  
    78  	return nil
    79  }
    80  
    81  // SetupLoggingLevel set logrus level
    82  func SetupLoggingLevel(level string) error {
    83  	lvl, err := logrus.ParseLevel(level)
    84  	if err != nil {
    85  		return fmt.Errorf("unable to set log level\n%v", err)
    86  	}
    87  
    88  	logrus.SetLevel(lvl)
    89  	return nil
    90  }