github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/prompts/storage/new.go (about)

     1  package storagePrompts
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/taubyte/go-project-schema/common"
     7  	structureSpec "github.com/taubyte/go-specs/structure"
     8  	storageLib "github.com/taubyte/tau-cli/lib/storage"
     9  	"github.com/taubyte/tau-cli/prompts"
    10  	"github.com/urfave/cli/v2"
    11  )
    12  
    13  func New(ctx *cli.Context) (*structureSpec.Storage, error) {
    14  	storage := &structureSpec.Storage{}
    15  
    16  	taken, err := storageLib.List()
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	storage.Name = prompts.GetOrRequireAUniqueName(ctx, NamePrompt, taken)
    22  	storage.Description = prompts.GetOrAskForADescription(ctx)
    23  	storage.Tags = prompts.GetOrAskForTags(ctx)
    24  
    25  	storage.Regex = prompts.GetMatchRegex(ctx)
    26  	storage.Match = GetOrRequireAMatch(ctx)
    27  	storage.Public = GetPublic(ctx)
    28  
    29  	size, err := common.StringToUnits(prompts.GetSizeAndType(ctx, "", true))
    30  	if err != nil {
    31  		// TODO verbose
    32  		return nil, err
    33  	}
    34  	storage.Size = uint64(size)
    35  
    36  	// Streaming or Object
    37  	storage.Type = SelectABucket(ctx)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	switch storage.Type {
    42  	case storageLib.BucketStreaming:
    43  		err = newStreaming(ctx, storage)
    44  	case storageLib.BucketObject:
    45  		err = newObject(ctx, storage)
    46  	default:
    47  		// Should not get here
    48  		return nil, fmt.Errorf("invalid bucket: %s", storage.Type)
    49  	}
    50  
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return storage, err
    56  }
    57  
    58  func newStreaming(ctx *cli.Context, storage *structureSpec.Storage) error {
    59  	var err error
    60  	storage.Ttl, err = prompts.GetOrRequireATimeout(ctx)
    61  	if err != nil {
    62  		// TODO verbose error i18n
    63  		return err
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func newObject(ctx *cli.Context, storage *structureSpec.Storage) error {
    70  	storage.Versioning = GetVersioning(ctx)
    71  	return nil
    72  }