github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/actions/creds.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     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  package actions
    16  
    17  import (
    18  	"github.com/dolthub/dolt/go/cmd/dolt/errhand"
    19  	"github.com/dolthub/dolt/go/libraries/doltcore/creds"
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    21  )
    22  
    23  func NewCredsFile(dEnv *env.DoltEnv) (string, creds.DoltCreds, errhand.VerboseError) {
    24  	credsDir, verr := EnsureCredsDir(dEnv)
    25  	if verr != nil {
    26  		return "", creds.EmptyCreds, verr
    27  	}
    28  
    29  	dCreds, verr := GenCredsWithVErr()
    30  
    31  	if verr != nil {
    32  		return "", creds.EmptyCreds, verr
    33  	}
    34  
    35  	credsPath, err := creds.JWKCredsWriteToDir(dEnv.FS, credsDir, dCreds)
    36  
    37  	if err != nil {
    38  		return "", creds.EmptyCreds, errhand.BuildDError("failed to create new key.").AddCause(err).Build()
    39  	}
    40  
    41  	return credsPath, dCreds, verr
    42  }
    43  
    44  func EnsureCredsDir(dEnv *env.DoltEnv) (string, errhand.VerboseError) {
    45  	credsPath, err := dEnv.CredsDir()
    46  	if err != nil {
    47  		return "", errhand.BuildDError("fatal: could not determine credentials dir").AddCause(err).Build()
    48  	}
    49  
    50  	err = dEnv.FS.MkDirs(credsPath)
    51  
    52  	if err != nil {
    53  		return "", errhand.BuildDError("fatal: failed to create credentials dir.").AddCause(err).Build()
    54  	}
    55  
    56  	return credsPath, nil
    57  }
    58  
    59  func GenCredsWithVErr() (creds.DoltCreds, errhand.VerboseError) {
    60  	dCreds, err := creds.GenerateCredentials()
    61  
    62  	if err != nil {
    63  		verr := errhand.BuildDError("").Build()
    64  		return dCreds, verr
    65  	}
    66  
    67  	return dCreds, nil
    68  }