github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/singletons/session/load.go (about)

     1  package session
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  
     7  	"github.com/taubyte/go-seer"
     8  	"github.com/taubyte/tau-cli/common"
     9  	"github.com/taubyte/tau-cli/constants"
    10  	singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons"
    11  	"github.com/taubyte/utils/fs/file"
    12  )
    13  
    14  func loadSession() (err error) {
    15  	loc, isSet := os.LookupEnv(constants.TauSessionLocationEnvVarName)
    16  	if !isSet {
    17  		loc, err = discoverOrCreateConfigFileLoc()
    18  		if err != nil {
    19  			return singletonsI18n.SessionCreateFailed(loc, err)
    20  		}
    21  	} else {
    22  		sessionFileLoc := path.Join(loc, sessionFileName+".yaml")
    23  
    24  		if !file.Exists(sessionFileLoc) {
    25  			err = os.MkdirAll(loc, common.DefaultDirPermission)
    26  			if err != nil {
    27  				return singletonsI18n.CreatingSessionFileFailed(err)
    28  			}
    29  
    30  			err = os.WriteFile(sessionFileLoc, nil, common.DefaultFilePermission)
    31  			if err != nil {
    32  				return singletonsI18n.CreatingSessionFileFailed(err)
    33  			}
    34  
    35  		}
    36  	}
    37  
    38  	return LoadSessionInDir(loc)
    39  }
    40  
    41  // Used in tests for confirming values were set
    42  func LoadSessionInDir(loc string) error {
    43  	if len(loc) == 0 {
    44  		return singletonsI18n.SessionFileLocationEmpty()
    45  	}
    46  
    47  	_seer, err := seer.New(seer.SystemFS(loc))
    48  	if err != nil {
    49  		return singletonsI18n.CreatingSeerAtLocFailed(loc, err)
    50  	}
    51  
    52  	_session = &tauSession{
    53  		root: _seer,
    54  	}
    55  
    56  	return nil
    57  }