#!/bin/bash set -e # Converts a raw collectd template to a go file with the template as a variable # named "CollectdTemplate". Should be called from the directory of the monitor # package. template_filename=$1 target_file=${2:-template.go} package=$(basename $(pwd)) old_content=$(cat $target_file 2>/dev/null || echo -n "") # A bunch of hacks so that we can render the template into the collectd package # and not have an import from the same package. collectd_package="collectd." collectd_package_import=' "github.com/signalfx/signalfx-agent/internal/monitors/collectd"' if [[ $package == "collectd" ]]; then collectd_package_import= collectd_package= fi new_content=$(cat <<-EOH // +build linux package $package // AUTOGENERATED BY scripts/collectd-template-to-go. DO NOT EDIT!! import ( "text/template"$collectd_package_import ) // CollectdTemplate is a template for a $package collectd config file var CollectdTemplate = template.Must(${collectd_package}InjectTemplateFuncs(template.New("$package")).Parse(\` $(cat $template_filename) \`)).Option("missingkey=error") EOH ) # Don't write content if it's the same so we don't trigger a recompilation. [[ $old_content == $new_content ]] || echo -n "$new_content" | gofmt > $target_file