11package conf
22
33import (
4- "bytes"
5- "encoding/binary"
64 "fmt"
75 "io"
86 "os"
@@ -27,12 +25,7 @@ func (c *ConfigType) UseProxy() bool {
2725
2826// Initializing configuration information
2927func InitConfig (path string ) error {
30- // Firstly, read the config info from executable file
31- if readFromBinary () == nil {
32- return nil
33- }
34-
35- // Secondly, it reads config.yml from the given path.
28+ // Firstly, it reads config.yml from the given path.
3629 // If there is no config.yml in the given path, it reads it from the default config path.
3730 _ , err := os .Stat (path )
3831 switch os .IsNotExist (err ) {
@@ -54,67 +47,6 @@ func InitConfig(path string) error {
5447 return nil
5548}
5649
57- // Read config from binary in the end
58- // config_bytes: n bytes
59- // end_magic: 10 bytes
60- // size: 4 bytes
61- // -----------------------------------
62- // | config_bytes | size | end_magic |
63- // -----------------------------------
64- func readFromBinary () error {
65- f , err := os .Open (os .Args [0 ])
66- if err != nil {
67- return err
68- }
69- defer f .Close ()
70- stat , err := f .Stat ()
71- if err != nil {
72- return err
73- }
74-
75- var end_magic = make ([]byte , 10 )
76- n , err := f .ReadAt (end_magic , stat .Size ()- 10 )
77- if err != nil {
78- return err
79- }
80-
81- if n != 10 {
82- return fmt .Errorf ("read end_magic err: %d" , n )
83- }
84-
85- // Not have `config.yml` in the end
86- if ! bytes .Equal (end_magic , []byte ("config.yml" )) {
87- return fmt .Errorf ("not a pikpakcli binary" )
88- }
89-
90- var size = make ([]byte , 4 )
91- n , err = f .ReadAt (size , stat .Size ()- 14 )
92-
93- if err != nil {
94- return err
95- }
96-
97- if n != 4 {
98- return fmt .Errorf ("read size err: %d" , n )
99- }
100-
101- configSize := int64 (binary .LittleEndian .Uint32 (size ))
102- configBuf := make ([]byte , configSize )
103-
104- n , err = f .ReadAt (configBuf , stat .Size ()- 14 - configSize )
105-
106- if err != nil || n != int (configSize ) {
107- return err
108- }
109-
110- if n != int (configSize ) {
111- return fmt .Errorf ("read config size err: %d" , n )
112- }
113-
114- // Unmarshal config
115- return yaml .Unmarshal (configBuf , & Config )
116- }
117-
11850// Read configuration file from the given path
11951func readFromPath (path string ) error {
12052 f , err := os .Open (path )
0 commit comments