How to Generate a Customized Bash Script Using Go Templates, Switch-Case Statements, and YAML Data?

Barbara Streisand
Release: 2024-10-29 04:18:29
Original
180 people have browsed it

How to Generate a Customized Bash Script Using Go Templates, Switch-Case Statements, and YAML Data?

Golang Template with Switch & ForEach

Problem: Construct a bash script from a Golang program that iterates over a list of dependencies, checks their types, and prints customized commands based on those types using switch-case statements.

Answer:

The following updated Golang code accomplishes the desired behavior:

<code class="go">package main

import (
    "log"
    "text/template"
    "gopkg.in/yaml.v2"
    "os"
)

type File struct {
    TypeVersion string `yaml:"_type-version"`
    Dependency []Dependency
}

type Dependency struct {
    Name    string
    Type    string
}

func main() {
    f := File{}

    err := yaml.Unmarshal([]byte(data), &f)
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    const t = `
#!/bin/bash
{{range .Dependency}}
echo "type is {{.Type}}"
{{switch .Type}}
  case "runner1":
    echo "Submitting api1"
  case "runner2":
    echo "Submitting api2"
  default:
    echo "Unknown type"
{{end}}
{{end}}
`

    tt := template.Must(template.New("").Parse(t))
    err = tt.Execute(os.Stdout, f)
    if err != nil {
        log.Println("executing template:", err)
    }
}</code>
Copy after login

Explanation:

  • The code uses text/template to format the output string.
  • The range keyword iterates over the Dependency array.
  • Inside the loop, a switch-case block is used to customize the output based on the Type of the dependency.
  • The case statements handle specific Type values and print the desired commands.
  • The default case handles types that are not specifically defined.

Result:

The example bash script produced by this code is:

<code class="bash">#!/bin/bash

echo "type is runner"
Submitting api1


echo "type is runner2"
Submitting api2</code>
Copy after login

Additional Considerations:

  • The example YAML data is hard-coded in the data variable. In a real-world scenario, this could be loaded from a file or database.
  • The API mapping (from Type to command) could be implemented as a map or separate configuration file for improved flexibility.
  • The cwd and Install fields in the Dependency struct are unused in this solution, as they were not required for the demo.

The above is the detailed content of How to Generate a Customized Bash Script Using Go Templates, Switch-Case Statements, and YAML Data?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!