LoginSignup
2
0

More than 5 years have passed since last update.

設定ファイルで環境変数を使用したい

Posted at

前提

環境変数を設定ファイルで使いたかったので汎用的に使えそうなものを作ってみました。
コマンドとGoライブラリとして使えます。

使い方

YAML JSON TOMLフォーマットでないと使えません。
使い方は書き換えたいファイルのパスを指定するだけです

変換前

$ cat example.yaml
glossary: 
  title: "example glossary"
  GlossDiv: 
    title: S
    GlossList: 
      GlossEntry: 
        ID: "$ID"
        SortAs: "$ID"
        GlossTerm: "Standard Generalized Markup Language"
        Acronym: "$ID"
        Abbrev: "ISO 8879:1986"
        GlossDef: 
          para: "A meta-markup language, used to create markup languages such as DocBook."
          GlossSeeAlso: 
            - GML
            - XML
        GlossSee: markup

変換後

$ export ID=SGML
$ envexpand example.yaml
glossary:
  title: "example glossary"
  GlossDiv:
    title: S
    GlossList:
      GlossEntry:
        ID: "SGML"
        SortAs: "SGML"
        GlossTerm: "Standard Generalized Markup Language"
        Acronym: "SGML"
        Abbrev: "ISO 8879:1986"
        GlossDef:
          para: "A meta-markup language, used to create markup languages such as DocBook."
          GlossSeeAlso:
            - GML
            - XML
        GlossSee: markup

ライブラリとしても使用できます
再帰的に文字列のFieldを探して置換処理をします

    data := ABC{
        A: "$A",
        B: []string{
            "$B",
            "$B",
            "$B",
        },
        D: &D{
            F: &F{
                I: []*I{
                    {
                        J: "$J",
                    },
                    {
                        J: "$J",
                        K: []map[int]string{
                            {
                                1: "$K",
                                2: "$K",
                            },
                        },
                        L: []string{
                            "$L",
                            "$L",
                        },
                    },
                },
            },
        },
    }
$ echo A=$A B=$B J=$J K=$K L=$L
A=AAAAA B=BBBBB J=JJJJJ K=KKKKK L=LLLLL
main.ABC{
    A: "AAAAA",
    B: []string{
        "BBBBB",
        "BBBBB",
        "BBBBB",
    },
    C: map[int]string{},
    D: &main.D{
        E: "",
        F: &main.F{
            G: 0,
            H: "",
            I: []*main.I{
                &main.I{
                    J: "JJJJJ",
                    K: []map[int]string{},
                    L: []string{},
                },
                &main.I{
                    J: "JJJJJ",
                    K: []map[int]string{
                        map[int]string{
                            1: "KKKKK",
                            2: "KKKKK",
                        },
                    },
                    L: []string{
                        "LLLLL",
                        "LLLLL",
                    },
                },
            },
        },
    },
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0