diff --git a/.github/workflows/kuksa-go.yml b/.github/workflows/kuksa-go.yml index 948718b80..90a54ee91 100644 --- a/.github/workflows/kuksa-go.yml +++ b/.github/workflows/kuksa-go.yml @@ -43,4 +43,9 @@ jobs: export PATH=$PATH:$HOME/go/bin go generate . go test . + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + working-directory: kuksa_go_client diff --git a/kuksa-client/kuksa_client/grpc/__init__.py b/kuksa-client/kuksa_client/grpc/__init__.py index 7f6216580..45c536ac9 100644 --- a/kuksa-client/kuksa_client/grpc/__init__.py +++ b/kuksa-client/kuksa_client/grpc/__init__.py @@ -555,8 +555,8 @@ def _load_creds(self) -> Optional[grpc.ChannelCredentials]: if self.private_key and self.certificate_chain: private_key = self.private_key.read_bytes() certificate_chain = self.certificate_chain.read_bytes() - logger.info(f"Using private client key {self.private_key} " - f"and chain/certificate {self.certificate_chain}") + # As of today there is no option in KUKSA.val Databroker to require client authentication + logger.info("Using client private key and certificates, mutual TLS supported if supported by server") return grpc.ssl_channel_credentials(root_certificates, private_key, certificate_chain) else: logger.info(f"No client certificates provided, mutual TLS not supported!") diff --git a/kuksa_go_client/README.md b/kuksa_go_client/README.md index 3df6fab3b..a4bda34a7 100644 --- a/kuksa_go_client/README.md +++ b/kuksa_go_client/README.md @@ -79,3 +79,7 @@ go mod tidy This will update `go.mod`and `go.sum`. +## Linters + +Our Continuous Integration verifies that the code pass the [Golang Linter](https://golangci-lint.run/usage/install). +To avoid failing PR builds it is recommended to run the linter manually before creating a Pull Request. diff --git a/kuksa_go_client/client_test.go b/kuksa_go_client/client_test.go index f4bf95925..bbfa69b37 100644 --- a/kuksa_go_client/client_test.go +++ b/kuksa_go_client/client_test.go @@ -1,4 +1,3 @@ - //******************************************************************************** // Copyright (c) 2022 Contributors to the Eclipse Foundation // @@ -15,8 +14,8 @@ package main import ( - "testing" "github.com/eclipse/kuksa.val/kuksa_go_client/kuksa_client" + "testing" ) // Note: Go support two methods to write a string @@ -32,96 +31,171 @@ import ( func TestArrayParseNoQuote(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`[say hello, abc]`) - if len(array) != 2 { t.Fail()} - if array[0] != "say hello" { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != "say hello" { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayParseNoInsideQuote(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`["say hello","abc"]`) - if len(array) != 2 { t.Fail()} - if array[0] != "say hello" { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != "say hello" { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayParseNoInsideQuoteSingle(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`['say hello','abc']`) - if len(array) != 2 { t.Fail()} - if array[0] != "say hello" { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != "say hello" { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayParseDoubleQuote(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`["say \"hello\"","abc"]`) - if len(array) != 2 { t.Fail()} - if array[0] != `say "hello"` { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != `say "hello"` { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayParseSingleQuote(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`[say \'hello\',"abc"]`) - if len(array) != 2 { t.Fail()} - if array[0] != `say 'hello'` { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != `say 'hello'` { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } - + func TestArrayParseComma(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`["say, hello","abc"]`) - if len(array) != 2 { t.Fail()} - if array[0] != `say, hello` { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != `say, hello` { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArraySquare(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`[say hello[], abc]`) - if len(array) != 2 { t.Fail()} - if array[0] != `say hello[]` { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != `say hello[]` { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayEmptyStringQuoted(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`["", abc]`) - if len(array) != 2 { t.Fail()} - if array[0] != `` { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != `` { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestArrayEmptyStringNotQuoted(t *testing.T) { // First item shall be ignored array, _ := kuksa_client.GetArrayFromInput[string](`[, abc]`) - if len(array) != 1 { t.Fail()} - if array[0] != "abc" { t.Fail()} + if len(array) != 1 { + t.Fail() + } + if array[0] != "abc" { + t.Fail() + } } func TestDoubleComma(t *testing.T) { // In this case the middle item is ignored array, _ := kuksa_client.GetArrayFromInput[string](`[def,, abc]`) - if len(array) != 2 { t.Fail()} - if array[0] != "def" { t.Fail()} - if array[1] != "abc" { t.Fail()} + if len(array) != 2 { + t.Fail() + } + if array[0] != "def" { + t.Fail() + } + if array[1] != "abc" { + t.Fail() + } } func TestQuotesInStringValues(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string](`["dtc1, dtc2", dtc3, \" dtc4, dtc4\"]`) - if len(array) != 4 { t.Fail()} - if array[0] != "dtc1, dtc2" { t.Fail()} - if array[1] != "dtc3" { t.Fail()} - if array[2] != "\" dtc4" { t.Fail()} - if array[3] != "dtc4\"" { t.Fail()} + if len(array) != 4 { + t.Fail() + } + if array[0] != "dtc1, dtc2" { + t.Fail() + } + if array[1] != "dtc3" { + t.Fail() + } + if array[2] != "\" dtc4" { + t.Fail() + } + if array[3] != "dtc4\"" { + t.Fail() + } } func TestQuotesInStringValues2(t *testing.T) { array, _ := kuksa_client.GetArrayFromInput[string]("['dtc1, dtc2', dtc3, \" dtc4, dtc4\"]") - if len(array) != 3 { t.Fail()} - if array[0] != "dtc1, dtc2" { t.Fail()} - if array[1] != "dtc3" { t.Fail()} - if array[2] != " dtc4, dtc4" { t.Fail()} + if len(array) != 3 { + t.Fail() + } + if array[0] != "dtc1, dtc2" { + t.Fail() + } + if array[1] != "dtc3" { + t.Fail() + } + if array[2] != " dtc4, dtc4" { + t.Fail() + } } - diff --git a/kuksa_go_client/kuksa_client/commn.go b/kuksa_go_client/kuksa_client/commn.go index a1e667de3..86b5fc722 100644 --- a/kuksa_go_client/kuksa_client/commn.go +++ b/kuksa_go_client/kuksa_client/commn.go @@ -130,7 +130,10 @@ func (cc *KuksaClientCommWs) startCommunication() error { for { req, ok := <-cc.sendChannel if ok { - cc.connSocket.WriteMessage(websocket.TextMessage, []byte(req)) + err := cc.connSocket.WriteMessage(websocket.TextMessage, []byte(req)) + if err != nil { + log.Fatal("WriteMessage error: ", err) + } } } }() diff --git a/kuksa_go_client/kuksa_client/grpc.go b/kuksa_go_client/kuksa_client/grpc.go index 8df21dd8b..1f4b2adf9 100644 --- a/kuksa_go_client/kuksa_client/grpc.go +++ b/kuksa_go_client/kuksa_client/grpc.go @@ -420,7 +420,11 @@ func (cg *KuksaClientCommGrpc) UnsubscribeFromKuksaVal(id string) error { cancel := *cg.cancel[id] cancel() client := *cg.subsChannel[id] - client.CloseSend() + err := client.CloseSend() + if err != nil { + log.Fatal("Error with CloseSend: ", err) + return err + } return nil } diff --git a/kuksa_go_client/main.go b/kuksa_go_client/main.go index e34b684a9..808cd788c 100644 --- a/kuksa_go_client/main.go +++ b/kuksa_go_client/main.go @@ -39,18 +39,18 @@ func main() { var backend kuksa_client.KuksaBackend -var token string -if *protocol == "ws" { - // example token from kuksa.val/kuksa_certificates/jwt/all-read-write.json.token - token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJrdWtzYS52YWwiLCJpc3MiOiJFY2xpcHNlIEtVS1NBIERldiIsImFkbWluIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTc2NzIyNTU5OSwia3Vrc2EtdnNzIjp7IioiOiJydyJ9fQ.QQcVR0RuRJIoasPXYsMGZhdvhLjUalk4GcRaxhh3-0_j3CtVSZ0lTbv_Z3As5BfIYzaMlwUzFGvCVOq2MXVjRK81XOAZ6wIsyKOxva16zjbZryr2V_m3yZ4twI3CPEzJch11_qnhInirHltej-tGg6ySfLaTYeAkw4xYGwENMBBhN5t9odANpScZP_xx5bNfwdW1so6FkV1WhpKlCywoxk_vYZxo187d89bbiu-xOZUa5D-ycFkd1-1rjPXLGE_g5bc4jcQBvNBc-5FDbvt4aJlTQqjpdeppxhxn_gjkPGIAacYDI7szOLC-WYajTStbksUju1iQCyli11kPx0E66me_ZVwOX07f1lRF6D2brWm1LcMAHM3bQUK0LuyVwWPxld64uSAEsvSKsRyJERc7nZUgLf7COnUrrkxgIUNjukbdT2JVN_I-3l3b4YXg6JVD7Y5g0QYBKgXEFpZrDbBVhzo7PXPAhJD6-c3DcUQyRZExbrnFV56RwWuExphw8lYnbMvxPWImiVmB9nRVgFKD0TYaw1sidPSSlZt8Uw34VZzHWIZQAQY0BMjR33fefg42XQ1YzIwPmDx4GYXLl7HNIIVbsRsibKaJnf49mz2qnLC1K272zXSPljO11Ke1MNnsnKyUH7mcwEs9nhTsnMgEOx_TyMLRYo-VEHBDLuEOiBo" - backend = &kuksa_client.KuksaClientCommWs{Config: &configKuksaClient} -} else if *protocol == "grpc" { - // example token from kuksa.val/jwt/provide-all.token - token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJsb2NhbCBkZXYiLCJpc3MiOiJjcmVhdGVUb2tlbi5weSIsImF1ZCI6WyJrdWtzYS52YWwiXSwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3NjcyMjU1OTksInNjb3BlIjoicHJvdmlkZSJ9.OJWzTvDjcmeWyg3vmBR5TEtqYaHq8HrpFLlTKZAfDBAQBUHpyUEboJ97jfWuWgBnTpnfboyfAbwvLqo6bEVZ6tXzF8n9LtW6HmPbIWoDqXuobM2grUCVaGKuOcnCpMCQYChziqHbYwRJYP9nkYgbQU1kE4dN7880Io4xzq0GEbWksB2CVpOoExQUmCZpCohPs-XEkdmXhcUKnWnOeiSsRGKusx987vpY_WOXh6WE7DfJgzAgpPDo33qI7zQuTzUILORQsiHmsrQO0-zcvokNjaQUzlt5ETZ7MQLCtiUQaN0NMbDMCWkmSfNvZ5hKCNbfr2FaiMzrGBOQdvQiFo-DqZKGNweaGpufYXuaKfn3SXKoDr8u1xDE5oKgWMjxDR9pQYGzIF5bDXITSywCm4kN5DIn7e2_Ga28h3rBl0t0ZT0cwlszftQRueDTFcMns1u9PEDOqf7fRrhjq3zqpxuMAoRANVd2z237eBsS0AvdSIxL52N4xO8P_h93NN8Vaum28fTPxzm8p9WlQh4mgUelggtT415hLcxizx15ARIRG0RiW91Pglzt4WRtXHnsg93Ixd3yXXzZ2i4Y0hqhj_L12SsXunK2VxKup2sFCQz6wM-t_7ADmNYcs80idzsadY8rYKDV8N1WqOOd4ANG_nzWa86Tyu6wAwhDVag5nbFmLZQ" - backend = &kuksa_client.KuksaClientCommGrpc{Config: &configKuksaClient} -} else { - log.Println("Specify -protocol=ws or -protocol=grpc") -} + var token string + if *protocol == "ws" { + // example token from kuksa.val/kuksa_certificates/jwt/all-read-write.json.token + token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJrdWtzYS52YWwiLCJpc3MiOiJFY2xpcHNlIEtVS1NBIERldiIsImFkbWluIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTc2NzIyNTU5OSwia3Vrc2EtdnNzIjp7IioiOiJydyJ9fQ.QQcVR0RuRJIoasPXYsMGZhdvhLjUalk4GcRaxhh3-0_j3CtVSZ0lTbv_Z3As5BfIYzaMlwUzFGvCVOq2MXVjRK81XOAZ6wIsyKOxva16zjbZryr2V_m3yZ4twI3CPEzJch11_qnhInirHltej-tGg6ySfLaTYeAkw4xYGwENMBBhN5t9odANpScZP_xx5bNfwdW1so6FkV1WhpKlCywoxk_vYZxo187d89bbiu-xOZUa5D-ycFkd1-1rjPXLGE_g5bc4jcQBvNBc-5FDbvt4aJlTQqjpdeppxhxn_gjkPGIAacYDI7szOLC-WYajTStbksUju1iQCyli11kPx0E66me_ZVwOX07f1lRF6D2brWm1LcMAHM3bQUK0LuyVwWPxld64uSAEsvSKsRyJERc7nZUgLf7COnUrrkxgIUNjukbdT2JVN_I-3l3b4YXg6JVD7Y5g0QYBKgXEFpZrDbBVhzo7PXPAhJD6-c3DcUQyRZExbrnFV56RwWuExphw8lYnbMvxPWImiVmB9nRVgFKD0TYaw1sidPSSlZt8Uw34VZzHWIZQAQY0BMjR33fefg42XQ1YzIwPmDx4GYXLl7HNIIVbsRsibKaJnf49mz2qnLC1K272zXSPljO11Ke1MNnsnKyUH7mcwEs9nhTsnMgEOx_TyMLRYo-VEHBDLuEOiBo" + backend = &kuksa_client.KuksaClientCommWs{Config: &configKuksaClient} + } else if *protocol == "grpc" { + // example token from kuksa.val/jwt/provide-all.token + token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJsb2NhbCBkZXYiLCJpc3MiOiJjcmVhdGVUb2tlbi5weSIsImF1ZCI6WyJrdWtzYS52YWwiXSwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3NjcyMjU1OTksInNjb3BlIjoicHJvdmlkZSJ9.OJWzTvDjcmeWyg3vmBR5TEtqYaHq8HrpFLlTKZAfDBAQBUHpyUEboJ97jfWuWgBnTpnfboyfAbwvLqo6bEVZ6tXzF8n9LtW6HmPbIWoDqXuobM2grUCVaGKuOcnCpMCQYChziqHbYwRJYP9nkYgbQU1kE4dN7880Io4xzq0GEbWksB2CVpOoExQUmCZpCohPs-XEkdmXhcUKnWnOeiSsRGKusx987vpY_WOXh6WE7DfJgzAgpPDo33qI7zQuTzUILORQsiHmsrQO0-zcvokNjaQUzlt5ETZ7MQLCtiUQaN0NMbDMCWkmSfNvZ5hKCNbfr2FaiMzrGBOQdvQiFo-DqZKGNweaGpufYXuaKfn3SXKoDr8u1xDE5oKgWMjxDR9pQYGzIF5bDXITSywCm4kN5DIn7e2_Ga28h3rBl0t0ZT0cwlszftQRueDTFcMns1u9PEDOqf7fRrhjq3zqpxuMAoRANVd2z237eBsS0AvdSIxL52N4xO8P_h93NN8Vaum28fTPxzm8p9WlQh4mgUelggtT415hLcxizx15ARIRG0RiW91Pglzt4WRtXHnsg93Ixd3yXXzZ2i4Y0hqhj_L12SsXunK2VxKup2sFCQz6wM-t_7ADmNYcs80idzsadY8rYKDV8N1WqOOd4ANG_nzWa86Tyu6wAwhDVag5nbFmLZQ" + backend = &kuksa_client.KuksaClientCommGrpc{Config: &configKuksaClient} + } else { + log.Println("Specify -protocol=ws or -protocol=grpc") + } err := backend.ConnectToKuksaVal() if err != nil { @@ -58,11 +58,11 @@ if *protocol == "ws" { } defer backend.Close() -//Authorize the connection -err = backend.AuthorizeKuksaValConn(token) -if err != nil { - log.Fatalf("Authorization Error: %v", err) -} + // Authorize the connection + err = backend.AuthorizeKuksaValConn(token) + if err != nil { + log.Fatalf("Authorization Error: %v", err) + } err = backend.SetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "true", "value") if err != nil { @@ -76,12 +76,12 @@ if err != nil { log.Printf("Get Value Error: %v", err) } else { for _, value := range values { - if *protocol == "grpc"{ + if *protocol == "grpc" { log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(*v1.DataEntry).String()) - }else{ + } else { log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(string)) } - + } } @@ -97,9 +97,9 @@ if err != nil { log.Printf("Get Value Error: %v", err) } else { for _, value := range values { - if *protocol == "grpc"{ - log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String()) - }else{ + if *protocol == "grpc" { + log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String()) + } else { log.Println("Vehicle.OBD.DTCList: " + value.(string)) } } @@ -124,15 +124,14 @@ if err != nil { log.Printf("Get Value Error: %v", err) } else { for _, value := range values { - if *protocol == "grpc"{ - log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String()) - }else{ + if *protocol == "grpc" { + log.Println("Vehicle.OBD.DTCList: " + value.(*v1.DataEntry).String()) + } else { log.Println("Vehicle.OBD.DTCList: " + value.(string)) - } + } } } - err = backend.SetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "true", "targetValue") if err != nil { log.Printf("Set Value Error: %v", err) @@ -140,45 +139,45 @@ if err != nil { log.Printf("Vehicle.ADAS.ABS.IsEnabled Set: true") } -tValues, err := backend.GetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "targetValue") -if err != nil { - log.Printf("Get Value Error: %v", err) -} else { - for _, value := range tValues { - if *protocol == "grpc"{ - log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(*v1.DataEntry).String()) - }else{ - log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(string)) - } + tValues, err := backend.GetValueFromKuksaVal("Vehicle.ADAS.ABS.IsEnabled", "targetValue") + if err != nil { + log.Printf("Get Value Error: %v", err) + } else { + for _, value := range tValues { + if *protocol == "grpc" { + log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(*v1.DataEntry).String()) + } else { + log.Println("Vehicle.ADAS.ABS.IsEnabled: " + value.(string)) + } + } } -} -// Get MetaData of Vehicle.Speed -value, err := backend.GetMetadataFromKuksaVal("Vehicle.Speed") -if err == nil { - for _, val := range value { - if *protocol == "grpc"{ - log.Println("Vehicle.Speed Metadata: " + val.(*v1.DataEntry).String()) - }else{ - log.Println("Vehicle.Speed Metadata: " + val.(string)) - } + // Get MetaData of Vehicle.Speed + value, err := backend.GetMetadataFromKuksaVal("Vehicle.Speed") + if err == nil { + for _, val := range value { + if *protocol == "grpc" { + log.Println("Vehicle.Speed Metadata: " + val.(*v1.DataEntry).String()) + } else { + log.Println("Vehicle.Speed Metadata: " + val.(string)) + } + } + } else { + log.Printf("Error while getting metadata: %s", err) } -} else { - log.Printf("Error while getting metadata: %s", err) -} -//Subscribe to Vehicle.Speed + //Subscribe to Vehicle.Speed -id, err := backend.SubscribeFromKuksaVal("Vehicle.Speed", "value") -if err == nil { - log.Printf("Vehicle.Speed Subscription Id: %s", id) -} else { - log.Printf("Subscription Error %s", err) -} -err = backend.PrintSubscriptionMessages(id) -if err != nil { - log.Printf("Printing the subscription messages failed with: %s", err) -} + id, err := backend.SubscribeFromKuksaVal("Vehicle.Speed", "value") + if err == nil { + log.Printf("Vehicle.Speed Subscription Id: %s", id) + } else { + log.Printf("Subscription Error %s", err) + } + err = backend.PrintSubscriptionMessages(id) + if err != nil { + log.Printf("Printing the subscription messages failed with: %s", err) + } } // More subscribing examples @@ -219,4 +218,4 @@ if err != nil { // log.Printf("Unsubscribing failed with: %s", err) // } // } -// } \ No newline at end of file +// } diff --git a/kuksa_go_client/protocInstall/protocInstall.go b/kuksa_go_client/protocInstall/protocInstall.go index 00105acf2..acfda360b 100755 --- a/kuksa_go_client/protocInstall/protocInstall.go +++ b/kuksa_go_client/protocInstall/protocInstall.go @@ -13,9 +13,9 @@ import ( const ( Name = "protoc" ZipFileName = Name + ".zip" - OsMac = "darwin" - OsWindows = "windows" - OsLinux = "linux" + OsMac = "darwin" + OsWindows = "windows" + OsLinux = "linux" ) func DownloadPackage(url string) error { @@ -23,13 +23,13 @@ func DownloadPackage(url string) error { if err != nil { return err } - defer resp.Body.Close() + defer resp.Body.Close() out, err := os.Create(ZipFileName) - if err != nil { + if err != nil { return err } - defer out.Close() + defer out.Close() if _, err := io.Copy(out, resp.Body); err != nil { return err @@ -38,26 +38,29 @@ func DownloadPackage(url string) error { } func UnzipSource(source, destination string) error { - reader, err := zip.OpenReader(source) - if err != nil { - return err - } - defer reader.Close() - - destination, err = filepath.Abs(destination) - if err != nil { - return err - } - - for _, f := range reader.File { + reader, err := zip.OpenReader(source) + if err != nil { + return err + } + defer reader.Close() + + destination, err = filepath.Abs(destination) + if err != nil { + return err + } + + for _, f := range reader.File { filePath := filepath.Join(destination, f.Name) if f.FileInfo().IsDir() { - os.MkdirAll(filePath, os.ModePerm) + err := os.MkdirAll(filePath, os.ModePerm) + if err != nil { + return err + } continue } else { fmt.Println("unziping files to", filePath) - + out, err := os.Create(filePath) if err != nil { return err @@ -73,9 +76,9 @@ func UnzipSource(source, destination string) error { return err } } - } + } - return nil + return nil } var url = map[string]string{ @@ -97,14 +100,14 @@ func Install() error { bit := 32 << (^uint(0) >> 63) var downloadUrl string switch goos { - case OsMac: - downloadUrl = url[OsMac] - case OsWindows: - downloadUrl = url[fmt.Sprintf("%s_%d", OsWindows, bit)] - case OsLinux: - downloadUrl = url[fmt.Sprintf("%s_%d", OsLinux, bit)] - default: - return fmt.Errorf("unsupport OS: %q", goos) + case OsMac: + downloadUrl = url[OsMac] + case OsWindows: + downloadUrl = url[fmt.Sprintf("%s_%d", OsWindows, bit)] + case OsLinux: + downloadUrl = url[fmt.Sprintf("%s_%d", OsLinux, bit)] + default: + return fmt.Errorf("unsupport OS: %q", goos) } print("Downloading protobuf compiler...\n") @@ -115,11 +118,11 @@ func Install() error { var home string _, found := os.LookupEnv("HOME") - if found { + if found { home = os.Getenv("HOME") - } else { - return &NotFoundError{} - } + } else { + return &NotFoundError{} + } dest := filepath.Join(home, Name) @@ -130,11 +133,11 @@ func Install() error { func Exists() bool { var root string _, found := os.LookupEnv("HOME") - if found { + if found { root = os.Getenv("HOME") - } else { - return false - } + } else { + return false + } protocPath := filepath.Join(root, Name) if _, err := os.Stat(protocPath); os.IsNotExist(err) { return false @@ -146,12 +149,15 @@ func Exists() bool { func ProtoExists() { if _, err := os.Stat("proto"); os.IsNotExist(err) { print("no directory proto exists. Creating...\n") - os.MkdirAll("proto", os.ModePerm) + err := os.MkdirAll("proto", os.ModePerm) + if err != nil { + panic(err) + } } } func main() { - if !Exists(){ + if !Exists() { print("No installed protobuf compiler found! Installing... \n") err := Install() if err != nil { @@ -160,4 +166,4 @@ func main() { } ProtoExists() print("All done!\n") -} \ No newline at end of file +}