...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/scripts/generate-cloud-code-snippets/generate_snippets.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/scripts/generate-cloud-code-snippets

     1  // Copyright 2022 Google LLC
     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  // This program generates snippets for Cloud Code using our samples.
    16  
    17  package main
    18  
    19  import (
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  	"path"
    24  
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/snippet/snippetgeneration"
    26  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil"
    27  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  const outputFileMode = 0600
    32  const dirMode = 0700
    33  
    34  func main() {
    35  	// Clear all snippets in directory and ensure generated snippets are up-to-date
    36  	snippetPath := repo.GetResourcesSnippetsPath()
    37  	if err := os.RemoveAll(snippetPath); err != nil {
    38  		log.Fatalf("error deleting dir %v: %v", snippetPath, err)
    39  	}
    40  	if err := os.Mkdir(snippetPath, dirMode); err != nil {
    41  		log.Fatalf("error recreating dir %v: %v", snippetPath, err)
    42  	}
    43  
    44  	samplesPath := repo.GetResourcesSamplesPath()
    45  	resources, err := fileutil.SubdirsIn(samplesPath)
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  
    50  	for _, resource := range resources {
    51  		sampleFilePath, err := snippetgeneration.PathToSampleFileUsedForSnippets(resource)
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  
    56  		content, err := ioutil.ReadFile(sampleFilePath)
    57  		if err != nil {
    58  			log.Fatalf("error reading file: %v", err)
    59  		}
    60  
    61  		snippet, err := snippetgeneration.SnippifyResourceConfig(content)
    62  		if err != nil {
    63  			log.Fatal(err)
    64  		}
    65  
    66  		sampleFileName := path.Base(sampleFilePath)
    67  		err = outputSnippetToFile(sampleFileName, snippet)
    68  		if err != nil {
    69  			log.Fatalf("error writing snippet to file: %v", err)
    70  		}
    71  	}
    72  }
    73  
    74  func outputSnippetToFile(outputFileName string, s snippetgeneration.Snippet) error {
    75  	outputPath := path.Join(repo.GetResourcesSnippetsPath(), outputFileName)
    76  	b, err := yaml.Marshal(s)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	err = ioutil.WriteFile(outputPath, b, outputFileMode)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	return nil
    85  }
    86  

View as plain text