github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/organization/utils.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package organization
    21  
    22  import (
    23  	"encoding/json"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/pkg/errors"
    28  
    29  	"github.com/1aal/kubeblocks/pkg/cli/cmd/auth/authorize"
    30  	"github.com/1aal/kubeblocks/pkg/cli/util"
    31  )
    32  
    33  const (
    34  	CloudContextDir          = "cloud_context"
    35  	CurrentOrgAndContextFile = "current.json"
    36  	ContextFile              = "context.json"
    37  )
    38  
    39  func GetCurrentOrgAndContextFilePath() (string, error) {
    40  	cliHomeDir, err := util.GetCliHomeDir()
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  	filePath := filepath.Join(cliHomeDir, CloudContextDir, CurrentOrgAndContextFile)
    48  	return filePath, nil
    49  }
    50  
    51  func GetContextFilePath() (string, error) {
    52  	cliHomeDir, err := util.GetCliHomeDir()
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  	filePath := filepath.Join(cliHomeDir, CloudContextDir, ContextFile)
    57  	return filePath, nil
    58  }
    59  
    60  func GetToken() (string, error) {
    61  	v := os.Getenv("TEST_ENV")
    62  	if v == "true" {
    63  		return "test_token", nil
    64  	}
    65  
    66  	cached := authorize.NewKeyringCachedTokenProvider(nil)
    67  	tokenRes, err := cached.GetTokens()
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  	if tokenRes != nil {
    72  		return tokenRes.IDToken, nil
    73  	}
    74  	return "", errors.New("Failed to get token")
    75  }
    76  
    77  func GetCurrentOrgAndContext() (*CurrentOrgAndContext, error) {
    78  	filePath, err := GetCurrentOrgAndContextFilePath()
    79  	if err != nil {
    80  		return nil, errors.Wrapf(err, "Failed to get current organization and context file: %s", filePath)
    81  	}
    82  	data, err := os.ReadFile(filePath)
    83  	if err != nil {
    84  		return nil, errors.Wrapf(err, "Failed to read current organization and context file: %s", filePath)
    85  	}
    86  
    87  	var currentOrgAndContext CurrentOrgAndContext
    88  	err = json.Unmarshal(data, &currentOrgAndContext)
    89  	if err != nil {
    90  		return nil, errors.Wrap(err, "Invalid current organization and context format.")
    91  	}
    92  
    93  	return &currentOrgAndContext, nil
    94  }