github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/context/store/tlsstore.go (about)

     1  package store
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  const tlsDir = "tls"
    10  
    11  type tlsStore struct {
    12  	root string
    13  }
    14  
    15  func (s *tlsStore) contextDir(id contextdir) string {
    16  	return filepath.Join(s.root, string(id))
    17  }
    18  
    19  func (s *tlsStore) endpointDir(contextID contextdir, name string) string {
    20  	return filepath.Join(s.root, string(contextID), name)
    21  }
    22  
    23  func (s *tlsStore) filePath(contextID contextdir, endpointName, filename string) string {
    24  	return filepath.Join(s.root, string(contextID), endpointName, filename)
    25  }
    26  
    27  func (s *tlsStore) createOrUpdate(contextID contextdir, endpointName, filename string, data []byte) error {
    28  	epdir := s.endpointDir(contextID, endpointName)
    29  	parentOfRoot := filepath.Dir(s.root)
    30  	if err := os.MkdirAll(parentOfRoot, 0755); err != nil {
    31  		return err
    32  	}
    33  	if err := os.MkdirAll(epdir, 0700); err != nil {
    34  		return err
    35  	}
    36  	return ioutil.WriteFile(s.filePath(contextID, endpointName, filename), data, 0600)
    37  }
    38  
    39  func (s *tlsStore) getData(contextID contextdir, endpointName, filename string) ([]byte, error) {
    40  	data, err := ioutil.ReadFile(s.filePath(contextID, endpointName, filename))
    41  	if err != nil {
    42  		return nil, convertTLSDataDoesNotExist(endpointName, filename, err)
    43  	}
    44  	return data, nil
    45  }
    46  
    47  func (s *tlsStore) remove(contextID contextdir, endpointName, filename string) error {
    48  	err := os.Remove(s.filePath(contextID, endpointName, filename))
    49  	if os.IsNotExist(err) {
    50  		return nil
    51  	}
    52  	return err
    53  }
    54  
    55  func (s *tlsStore) removeAllEndpointData(contextID contextdir, endpointName string) error {
    56  	return os.RemoveAll(s.endpointDir(contextID, endpointName))
    57  }
    58  
    59  func (s *tlsStore) removeAllContextData(contextID contextdir) error {
    60  	return os.RemoveAll(s.contextDir(contextID))
    61  }
    62  
    63  func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFiles, error) {
    64  	epFSs, err := ioutil.ReadDir(s.contextDir(contextID))
    65  	if err != nil {
    66  		if os.IsNotExist(err) {
    67  			return map[string]EndpointFiles{}, nil
    68  		}
    69  		return nil, err
    70  	}
    71  	r := make(map[string]EndpointFiles)
    72  	for _, epFS := range epFSs {
    73  		if epFS.IsDir() {
    74  			epDir := s.endpointDir(contextID, epFS.Name())
    75  			fss, err := ioutil.ReadDir(epDir)
    76  			if err != nil {
    77  				return nil, err
    78  			}
    79  			var files EndpointFiles
    80  			for _, fs := range fss {
    81  				if !fs.IsDir() {
    82  					files = append(files, fs.Name())
    83  				}
    84  			}
    85  			r[epFS.Name()] = files
    86  		}
    87  	}
    88  	return r, nil
    89  }
    90  
    91  // EndpointFiles is a slice of strings representing file names
    92  type EndpointFiles []string
    93  
    94  func convertTLSDataDoesNotExist(endpoint, file string, err error) error {
    95  	if os.IsNotExist(err) {
    96  		return &tlsDataDoesNotExistError{endpoint: endpoint, file: file}
    97  	}
    98  	return err
    99  }