github.com/blend/go-sdk@v1.20220411.3/sourceutil/temp_dir.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sourceutil
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"time"
    14  )
    15  
    16  // CreateTempDir creates a temporary directory with a given prefix.
    17  func CreateTempDir(prefix string) (*TempDir, error) {
    18  	fullPath := fmt.Sprintf("%s_%d", prefix, time.Now().UTC().UnixNano())
    19  	if err := os.MkdirAll(fullPath, 0755); err != nil {
    20  		return nil, err
    21  	}
    22  	return &TempDir{fullPath}, nil
    23  }
    24  
    25  // TempDir is a directory that can be cleaned up with Close.
    26  type TempDir struct {
    27  	Path string
    28  }
    29  
    30  // Close removes the directory.
    31  func (td TempDir) Close() error {
    32  	return os.RemoveAll(td.Path)
    33  }