-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move config specific functions to their own package
- Loading branch information
Roland Shoemaker
committed
May 17, 2017
1 parent
d0c7380
commit 8b48ccf
Showing
5 changed files
with
102 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package configs | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"time" | ||
) | ||
|
||
// JSONDuration is an abstraction used to simplifying unmarshalling | ||
// json into structs that contain time.Duration fields. Expected | ||
// format is the same as provided to time.ParseDuration. | ||
type JSONDuration struct { | ||
time.Duration | ||
} | ||
|
||
// UnmarshalJSON parses a time.Duration field from a string | ||
func (jd *JSONDuration) UnmarshalJSON(data []byte) error { | ||
s := "" | ||
err := json.Unmarshal(data, &s) | ||
if err != nil { | ||
return err | ||
} | ||
d, err := time.ParseDuration(s) | ||
if err != nil { | ||
return err | ||
} | ||
jd.Duration = d | ||
return nil | ||
} | ||
|
||
// LoadConfig loads a configuration JSON file from the provided path and | ||
// unmarshals it into the provided object. It also enforces --config being | ||
// the only flag provided to the binary calling LoadConfig. | ||
func LoadConfig(path string, obj interface{}) error { | ||
// Only allow either --config OR any other flags | ||
if flag.CommandLine.NFlag() > 1 { | ||
return errors.New("Cannot use --config with any other flags") | ||
} | ||
f, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("Failed to read config file: %v", err) | ||
} | ||
if err := json.Unmarshal(f, obj); err != nil { | ||
return fmt.Errorf("Failed to parse config file: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters